仅选择子表连接的第一行

时间:2015-01-12 13:00:36

标签: mysql

我有桌子 goodsbooking_tbl goodsBooking_tbl-structure goodsbooking_subTbl

我需要什么:需要选择带有一些id的主表中的行,并且还只选择主表id的第一行..

尝试了这个..

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

它返回enter image description here

我只需要子表中的第一行即

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

提前感谢..

1 个答案:

答案 0 :(得分:1)

因为你只想要一列,我认为最简单的方法可能是相关的子查询:

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