skip to Main Content

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

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

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

Brand Provider Product URL
Lightning Pragmatic Play Madame Destiny Link
Lightning Isoftbet Halloween Jack Link
Lightning Netent Royal Potato Link

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

Please help me if anyboday can`

3

Answers


  1. replace your query with

    $sql = "SELECT * FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') GROUP BY  Provider;"; 
    
    Login or Signup to reply.
  2. Try this if u wish

    $sql = "SELECT * FROM tablename WHERE Brand='Coolcasino' and Provider IN ('Pragmatic Play','Isoftbet','Netent') GROUP BY  Provider, RAND()";
    
    Login or Signup to reply.
  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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search