Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql select distinct id's and select first row only

This is my table. I want to select distinct 'bill_no' with only first related row.

    +--------------+--------------+-------+------------+
    | 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 |     
    +--------------+-------+------+-------+------------+

I want data to be displayed like the below table having only distinct "bill_no" -

Output-

    +--------------+--------------+-------+------------+
    | 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 |     
    +--------------+-------+------+-------+------------+
like image 491
Nikz Avatar asked Feb 12 '23 13:02

Nikz


1 Answers

Use group by

select * from youtable group by bill_no
like image 181
Mahendra Jella Avatar answered Feb 14 '23 03:02

Mahendra Jella