我正在尝试显示一些合并4个表的信息。我的表是device,device_model,sub_product_area和borrow_device。 我遇到的问题是,当我使用LEFT JOIN将表链接到设备表时,来自borrow_device表的transaction_ID有一些重复。所以我想在transaction_Mode上为ORFT JOIN进行ORDER BY,并且只选择' red'在这种情况下,颜色相关的transaction_ID。我尝试过SELECT DISTINCT,但它没有用。没有类似的问题答案有效。
这是我的查询 - >
SELECT *
FROM device b
LEFT OUTER JOIN device_model a ON (b.`model_ID`=a.`model_ID`)
LEFT JOIN sub_product_area c ON (b.`sub_Product_Area_ID` = c.`sub_Product_Area_ID`)
LEFT JOIN borrow_device d ON (d.`device_ID` = b.`device_ID` and CURDATE() between from_Date and to_Date)
ORDER BY d.transaction_Mode DESC
My table structures are as below -:
提前致谢!
答案 0 :(得分:0)
device
表和sub_product_area
表有2个字段相关的是sub_product_area_id
和product_area_id
。在您的SQl语句中尝试添加“and b.product_Area_ID = c.product_Area_ID
”,因此您的语法将如下所示:
SELECT *
FROM device b
LEFT OUTER JOIN device_model a ON (b.`model_ID`=a.`model_ID`)
LEFT JOIN sub_product_area c ON (b.`sub_Product_Area_ID` = c.`sub_Product_Area_ID` and b.`product_Area_ID` = c.`product_Area_ID`)
LEFT JOIN borrow_device d ON (d.`device_ID` = b.`device_ID` and CURDATE() between from_Date and to_Date)
ORDER BY d.transaction_Mode DESC
答案 1 :(得分:0)
在最后添加'GROUP BY'解决了我的问题。谢谢大家!