Möchten Sie mit SQL und PHP nur Zeilen mit ausgewähltem Wert anzeigen?
P粉007288593
P粉007288593 2023-09-02 15:25:33
0
2
571
<p>Ich habe 40 Anbieter und 10.000 Produkte, möchte aber von jedem Anbieter 1 Produkt anzeigen</p> <table class="s-table"> <thead> <tr> <th>Marke</th> <th>Anbieter</th> <th>Produkte</th> <th>URL</th> </tr> </thead> <tbody> <tr> <td>Blitz</td> <td>Pragmatisches Spiel</td> <td>Mrs. Destiny</td> <td>Link</td> </tr> <tr> <td>Blitz</td> <td>Isoftbet</td> <td>Halloween Jack</td> <td>Link</td> </tr> <tr> <td>Blitz</td> <td>Pragmatisches Spiel</td> <td>Sweet Bonanza</td> <td>Link</td> </tr> <tr> <td>Blitz</td> <td>Isoftbet</td> <td>Tropical Security</td> <td>Link</td> </tr> <tr> <td>Blitz</td> <td>Netzwerk</td> <td>Royal Potato</td> <td>Link</td> </tr> <tr> <td>Blitz</td> <td>Netzwerk</td> <td>Mrs. Destiny</td> <td>Link</td> </tr> </tbody> </table> <p>Dies ist meine aktuelle SQL-Tabelle.Ich möchte aber 1 Element pro Anbieter anzeigen, zum Beispiel: </p> <table class="s-table"> <thead> <tr> <th>Marke</th> <th>Anbieter</th> <th>Produkte</th> <th>URL</th> </tr> </thead> <tbody> <tr> <td>Blitz</td> <td>Pragmatisches Spiel</td> <td>Mrs. Destiny</td> <td>Link</td> </tr> <tr> <td>Blitz</td> <td>Isoftbet</td> <td>Halloween Jack</td> <td>Link</td> </tr> <tr> <td>Blitz</td> <td>Netzwerk</td> <td>Royal Potato</td> <td>Link</td> </tr> </tbody> </table> <p>Das ist mein Code `</p> <pre class="brush:php;toolbar:false;"><?php /* Versuchen Sie, eine MySQL-Serververbindung herzustellen Server mit Standardeinstellung (Benutzer „root“ ohne Passwort) */ $link = mysqli_connect("localhost", "newuser1", "p,+Dn@auTD3$*G5", "newdatabse"); // Verbindung prüfen if($link === false){ die("FEHLER: Verbindung konnte nicht hergestellt werden. " . mysqli_connect_error()); }// Versuchen Sie, die Abfrage auszuführen $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>Marke</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['Marke'] . "</td>"; echo "<td>" . $row['Provider'] . "</td>"; echo "<td>" . $row['Produkt'] . "</td>"; echo "<td>" . $row['URL'] . "</td>"; echo "</tr>"; } echo "</table>"; // Ergebnismenge schließen mysqli_free_result($result); } anders{ echo "Es wurden keine Datensätze gefunden, die Ihrer Suchanfrage entsprechen."; } } anders{ echo „FEHLER: $sql konnte nicht ausgeführt werden. " . mysqli_error($link); } // Verbindung schließen mysqli_close($link); ?></pre> <p>如果有人可以的话请帮助我`</p>
P粉007288593
P粉007288593

Antworte allen(2)
P粉939473759

将您的查询替换为

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

使用行号:

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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!