我有 40 个提供商和 10,000 个产品,但我想显示每个提供商的 1 个产品
| 品牌 | 提供商 | 产品 | 网址 |
|---|---|---|---|
| 闪电 | 务实的游戏 | 命运夫人 | 链接 |
| 闪电 | Isoftbet | 万圣节杰克 | 链接 |
| 闪电 | 务实的游戏 | 甜蜜的富矿 | 链接 |
| 闪电 | Isoftbet | 热带保安 | 链接 |
| 闪电 | 网络 | 皇家马铃薯 | 链接 |
| 闪电 | 网络 | 命运夫人 | 链接 |
这就是我现在的 SQL 表。但我想显示每个提供商的 1 项,例如:
| 品牌 | 提供商 | 产品 | 网址 |
|---|---|---|---|
| 闪电 | 务实的游戏 | 命运夫人 | 链接 |
| 闪电 | Isoftbet | 万圣节杰克 | 链接 |
| 闪电 | 网络 | 皇家马铃薯 | 链接 |
这是我的代码 `
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "newuser1", "p,+Dn@auTD3$*G5", "newdatabse");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') ;";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Brand</th>";
echo "<th>Provider</th>";
echo "<th>Product</th>";
echo "<th>URL</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['Brand'] . "</td>";
echo "<td>" . $row['Provider'] . "</td>";
echo "<td>" . $row['Product'] . "</td>";
echo "<td>" . $row['URL'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
如果有人可以的话请帮助我`
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
将您的查询替换为
$sql = "SELECT * FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') GROUP BY Provider;";使用行号:
select Brand, Provider, Product, URL from ( select Brand, Provider, Product, URL, row_number() over(partition by Provider order by rand()) as row_num from tablename where Brand='Lightning' and Provider IN ('Pragmatic Play','Isoftbet','Netent') ) as rand_prod where row_num=1;https://dbfiddle.uk/BGzx6cYY
注意,我建议不要使用
select *,仅选择您真正需要的列