css 希望只显示sql和php中所选值的行[重复]

11dmarpk  于 2022-11-19  发布在  PHP
关注(0)|答案(3)|浏览(138)

此问题在此处已有答案

MySQL | How to select only one record of each category?(2个答案)
MYSQL Select One Random record from each Category(5个答案)
13个小时前关门了。
我有40个提供商和10,000个产品,但我希望显示每个提供商的1个产品
| 品牌名称|提供者|产品名称|网址|
| - -|- -|- -|- -|
| lightning |实用游戏|命运女神|连接|
| lightning |伊索特贝特|万圣节杰克|连接|
| lightning |实用游戏|甜蜜的富矿|连接|
| lightning |伊索特贝特|热带渤南|连接|
| lightning |内滕|皇家土豆|连接|
| lightning |内滕|命运女神|连接|
因此,这是我的SQL表现在。但我想显示1项的每个供应商,如:
| 品牌名称|提供者|产品名称|网址|
| - -|- -|- -|- -|
| lightning |实用游戏|命运女神|连接|
| lightning |伊索特贝特|万圣节杰克|连接|
| lightning |内滕|皇家土豆|连接|
这是我的密码

<?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);
?>

如果有人可以的话,请帮帮我

atmip9wb

atmip9wb1#

有没有可能1个产品随机显示?我的意思是,每次显示第一个产品的瞬间,它会显示不同的产品
使用行号(_N):

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 *,只选择您真正需要的列
0yg35tkg

0yg35tkg2#

将查询替换为

$sql = "SELECT * FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') GROUP BY  Provider;";
gudnpqoy

gudnpqoy3#

如果你愿意试试这个

$sql = "SELECT * FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') GROUP BY  Provider, RAND()";

相关问题