Mysql选择不同的id并仅选择第一行

时间:2014-11-18 09:16:11

标签: php mysql database mysqli

这是我的桌子。我想选择与第一个相关行不同的'bill_no'。

    +--------------+--------------+-------+------------+
    | id | bill_no | product_name | tax   | total      | 
    +--------------+-------+------+-------+------------+
    | 1  |    12   |   Hairgel    |  10   |        241 |      
    | 2  |    12   |  Spiker gel  |  10   |        300 |      
    | 3  |    13   | Wokzem amba  |  12   |        450 |      
    | 4  |    13   | test prod    |  1    |        145 |      
    | 5  |    14   | wokk         |  3    |         55 |   
    | 6  |    14   | Kamer hyp    |  11   |         46 |      
    | 7  |    15   | Xyombokx     |  2    |        220 |     
    +--------------+-------+------+-------+------------+

我希望数据显示如下表只有明显的“bill_no” -

输出 -

    +--------------+--------------+-------+------------+
    | id | bill_no | product_name | tax   | total      | 
    +--------------+-------+------+-------+------------+
    | 1  |    12   |   Hairgel    |  10   |        241 |                
    | 3  |    13   | Wokzem amba  |  12   |        450 |           
    | 5  |    14   | wokk         |  3    |         55 |         
    | 7  |    15   | Xyombokx     |  2    |        220 |     
    +--------------+-------+------+-------+------------+

3 个答案:

答案 0 :(得分:3)

使用group by

select * from youtable group by bill_no

答案 1 :(得分:2)

select t1.* 
from your_table t1
join
(
  select min(id) as id
  from your_table
  group by bill_no
) t2 on t1.id = t2.id

答案 2 :(得分:1)

您可以使用GROUP BY子句.GROUP BY子句始终按cloumn名称返回带有相关组的第一行。

SELECT * FROM `table_1` group by `bill_no`