Want to show only row of selected value with sql and php

Want to show only row of selected value with sql and php

Problem Description:

I have 40 provider and 10,000 product but i want to show 1 product of each provider

BrandProviderProductURL
LightningPragmatic PlayMadame DestinyLink
LightningIsoftbetHalloween JackLink
LightningPragmatic PlaySweet BonanzaLink
LightningIsoftbetTropical BonanLink
LightningNetentRoyal PotatoLink
LightningNetentMadame DestinyLink

SO this my SQL table now. But i want to show 1 item of each Provider like:

BrandProviderProductURL
LightningPragmatic PlayMadame DestinyLink
LightningIsoftbetHalloween JackLink
LightningNetentRoyal PotatoLink

this is my code
`


<?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,[email protected]$*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);
?>

Please help me if anyboday can`

Solution – 1

replace your query with

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

Solution – 2

Try this if u wish

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

Solution – 3

Is there any chance that 1 product show randomly? I mean every time it
will show a different product instant of showing the first product
every time

Use row_number :

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

Note, I suggest not using select * , select only the columns which you really need

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject