如何在mysql中进行完全外连接

时间:2016-03-07 15:48:58

标签: mysql sql vb.net join

我已经搜索过并且已经花了很多时间来进行此查询... 我想要的是我需要输出特定原材料的原材料名称,而其中一列是空的。我已经尝试过完全外连接,但我不知道为什么phpmyadmin不接受我的查询我已经尝试了几乎所有我能。

表:

purchaserequest_tbl

列:purchaserequestID,orderID,rawmaterialID,rawmaterialquantity

rawmaterial_tbl:

列:id,name,category,unit

这是查询

SELECT `purchaserequest_tbl`.`orderID` AS 'Order No', 
       `rawmaterial_tbl`.`rawMaterialName` AS 'Raw Material Name', 
       `purchaserequest_tbl`.`rawMaterialQty` AS 'Length (inches)'
FROM `purchaserequest_tbl`
LEFT OUTER JOIN `rawmaterial_tbl`
 ON `purchaserequest_tbl`.`rawMaterialID` = `rawmaterial_tbl`.`rawMaterialName`
WHERE `purchaserequest_tbl`.`purchaseRequestStatus` = 'Requested'

我希望有人能够帮助我......我真的很难过这里

1 个答案:

答案 0 :(得分:1)

如果我理解你,你发布的查询为你工作,但没有给出预期的结果。完全外连接不适合你,因为MYSQL不支持完全外连接,所以你必须做2个左外连接才能得到这个结果,试试这个:

SELECT `purchaserequest_tbl`.`orderID` AS 'Order No', 
       `rawmaterial_tbl`.`rawMaterialName` AS 'Raw Material Name', 
       `purchaserequest_tbl`.`rawMaterialQty` AS 'Length (inches)'
FROM `purchaserequest_tbl`
LEFT OUTER JOIN `rawmaterial_tbl`
ON `purchaserequest_tbl`.`rawMaterialID` = `rawmaterial_tbl`.`rawMaterialName`
WHERE `purchaserequest_tbl`.`purchaseRequestStatus` = 'Requested'
UNION
SELECT `purchaserequest_tbl`.`orderID` AS 'Order No', 
       `rawmaterial_tbl`.`rawMaterialName` AS 'Raw Material Name', 
       `purchaserequest_tbl`.`rawMaterialQty` AS 'Length (inches)'
FROM `rawmaterial_tbl`
LEFT OUTER JOIN `purchaserequest_tbl`
ON `purchaserequest_tbl`.`rawMaterialID` = `rawmaterial_tbl`.`rawMaterialName`
WHERE `purchaserequest_tbl`.`purchaseRequestStatus` = 'Requested'

如果只想要那些null,请在两个where子句中添加WHERE rightTable.Column为null