在桌子上有goodsbooking_tbl
我需要什么:需要选择具有一些ID的inn主表行,并且还仅选择主表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);
它返回
我只需要子表的第一行,即。
1 3 1 01-01-2015 GC-15-01 15000.00 safdasf
2 2 1 01-01-2015 GC-15-02 350.00 sdafsaf
提前致谢..
最佳答案
因为只需要一列,所以我认为最简单的方法可能是相关的子查询:
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);
关于mysql - 仅选择子表联接的第一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27902596/