简体   繁体   中英

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

你可以使用DISTINCT ,因为你只需要1列,

SELECT DISTINCT  Customer FROM `Orders`

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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM