简体   繁体   中英

select only first row of an sub table join

am having the table goodsbooking_tbl goodsBooking_tbl结构goodsbooking_subTbl

what i need: need to select the rows inn main table with some ids, and also select only the first row of the main table id..

was tryed this..

SELECT a.idgoodsbooking_tbl, a.consignorId, a.ConsigneeId, date_format(a.bookingDate,'%d-%m-%Y') as GCdate, 
a.GCNumber, a.frieghtTotal, a.paymentType,b.description FROM goodsbooking_tbl a 
 JOIN goodsbooking_subtbl b ON a.idgoodsbooking_tbl=b.idgoodsbooking_tbl WHERE a.idgoodsbooking_tbl in (1,2);

it returns 在此处输入图片说明

i need only the first row from the subtable ie.

1        3                1        01-01-2015     GC-15-01     15000.00     safdasf
2        2                1        01-01-2015     GC-15-02     350.00       sdafsaf

thanks in advance..

Because you want only one column, I think the easiest way might be a correlated subquery:

SELECT a.idgoodsbooking_tbl, a.consignorId, a.ConsigneeId,
       date_format(a.bookingDate,'%d-%m-%Y') as GCdate, 
       a.GCNumber, a.frieghtTotal, a.paymentType,
       (SELECT b.description
        FROM goodsbooking_subtbl b
        WHERE  a.idgoodsbooking_tbl = b.idgoodsbooking_tbl
        ORDER BY idgoodbooking_subtbl DESC
        LIMIT 1
       ) as description
FROM goodsbooking_tbl a
WHERE a.idgoodsbooking_tbl in (1,2);

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