连接两个表都有不同的限制

问题描述 投票:0回答:1

基本上 bill_details 表有 item_name 的多个值,我只想要最后一个,它是正确的,但 bill.rate 和 bill.disc 显示空值。每列和表的选择都是正确的。我已经通过不同的子查询进行了尝试,但我的数据库版本不允许我在子查询中使用任何内置函数,因此我无法在子查询中使用 top 或 max 函数。 如果除了左连接之外,您还有其他具有最小时间复杂度的方法,那将会很有帮助。

谢谢你,

SELECT item.*, 
       bill.rate, 
       bill.disc 
FROM item_details AS item 
LEFT JOIN ( SELECT id, 
                   item_name, 
                   rate, 
                   disc 
            FROM bill_details 
            WHERE bill_type='P' 
            ORDER BY id DESC LIMIT 1 
         ) AS bill ON item.id = bill.item_name 
WHERE item.flag='N' 
ORDER BY item.item_name ASC;
mysql left-join
1个回答
-1
投票
SELECT *?...
FROM item_details AS item
LEFT JOIN bill_details AS bill 
ON item.id = bill.item_name
WHERE bill.bill_type='P' AND item.flag='N' 
ORDER BY item.item_name ASC;
© www.soinside.com 2019 - 2024. All rights reserved.