Ricardo Capistran
Ricardo Capistran

Reputation: 161

Group items on SQL

Can someone help me with this:

I have a mySQL database and I would like to do a simple search amoung the items, here is an example of my database named "orders"

id  OrderDate   OrderPrice  Customer
1   2008/11/12  1000        Hansen
2   2008/10/23  1600        Nilsen
3   2008/09/02  700         Hansen
4   2008/09/03  300         Hansen
5   2008/08/30  2000        Jensen
6   2008/10/04  100         Nilsen

How can I group by duplicated data and display it like this:

<select name="costumer">
    <option>Hansen</option>
    <option>Nilsen</option>
    <option>Jensen</option>
</select>

I know it is the GROUP BY command, but dont know how to display it on a PHP script file

Upvotes: 1

Views: 64

Answers (2)

sephoy08
sephoy08

Reputation: 1114

Try this:

       $dbconn = new mysqli();
       $dbconn->connect("localhost","root","","test");

       if($dbconn->connect_errno ){
         echo "Connection Failed";
       }

       $query = "SELECT DISTINCT  Customer FROM `Orders`";

       $result = $dbconn->query($query);
       echo "<select name=\"costumer\">";
       while($row = $result->fetch_array())
       {
            echo "<option>".$row["Customer"]."</option>";
       }

      echo "</select>";

Upvotes: 2

John Woo
John Woo

Reputation: 263803

you can use DISTINCT on this since you only need to have 1 column,

SELECT DISTINCT  Customer FROM `Orders`

Upvotes: 2

Related Questions