如何用SQL和PHP实现每个Provider仅显示一条产品数据
需求与解决方案
需求说明
现有40个供应商(Provider)、10000条产品数据,需要实现每个供应商仅展示一条产品记录。
原始数据示例
| Brand | Provider | Product | URL |
|---|---|---|---|
| Lightning | Pragmatic Play | Madame Destiny | Link |
| Lightning | Isoftbet | Halloween Jack | Link |
| Lightning | Pragmatic Play | Sweet Bonanza | Link |
| Lightning | Isoftbet | Tropical Bonan | Link |
| Lightning | Netent | Royal Potato | Link |
| Lightning | Netent | Madame Destiny | Link |
期望展示效果
| Brand | Provider | Product | URL |
|---|---|---|---|
| Lightning | Pragmatic Play | Madame Destiny | Link |
| Lightning | Isoftbet | Halloween Jack | Link |
| Lightning | Netent | Royal Potato | Link |
修改后的PHP代码
核心改动是调整SQL查询语句,确保每个Provider只返回一条记录。以下提供两种兼容不同MySQL版本的方案:
方案1:兼容MySQL 5.x(使用GROUP BY)
<?php /* 连接MySQL服务器 */ $link = mysqli_connect("localhost", "newuser1", "p,+Dn@auTD3$*G5", "newdatabse"); // 检查连接 if($link === false){ die("ERROR: 连接失败。 " . mysqli_connect_error()); } // 执行查询:按Brand和Provider分组,取每个组的第一条产品记录(用MIN确保稳定取值) $sql = "SELECT Brand, Provider, MIN(Product) AS Product, MIN(URL) AS URL FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') GROUP BY Brand, Provider;"; 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>"; // 释放结果集 mysqli_free_result($result); } else{ echo "未找到匹配的记录。"; } } else{ echo "ERROR: 无法执行查询 $sql。 " . mysqli_error($link); } // 关闭连接 mysqli_close($link); ?>
方案2:兼容MySQL 8.0+(使用窗口函数,更灵活)
如果你的MySQL版本是8.0及以上,推荐用窗口函数精准控制取每个Provider的第一条记录:
<?php /* 连接MySQL服务器 */ $link = mysqli_connect("localhost", "newuser1", "p,+Dn@auTD3$*G5", "newdatabse"); // 检查连接 if($link === false){ die("ERROR: 连接失败。 " . mysqli_connect_error()); } // 执行查询:用ROW_NUMBER()给每个Provider的记录编号,取编号为1的记录 $sql = "SELECT Brand, Provider, Product, URL FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY Provider ORDER BY Product) AS rn FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') ) t WHERE rn=1;"; 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>"; // 释放结果集 mysqli_free_result($result); } else{ echo "未找到匹配的记录。"; } } else{ echo "ERROR: 无法执行查询 $sql。 " . mysqli_error($link); } // 关闭连接 mysqli_close($link); ?>
说明
- 方案1通过
GROUP BY按供应商分组,用MIN()函数取每组的第一条产品数据,兼容旧版本MySQL; - 方案2用
ROW_NUMBER()窗口函数,可通过ORDER BY指定取哪一条记录(比如按产品名称排序取第一个),控制更精准,适合MySQL 8.0及以上版本。
内容的提问来源于stack exchange,提问作者sobuz islam
相关产品推荐
相关产品推荐

