mysql查询具有不同数据的两个表

时间:2018-10-24 22:15:06

标签: mysql

我有两个表(material_bom)和(recvdmaterials)。 (material_bom)用于订购材料,(recvdmaterials)用于接收材料。如果两个表都具有联接函数中提到的相同信息,则下面的查询有效。但是,如果(recvdmaterials)中没有任何内容,则不会显示该行。

SELECT
material_bom.job_ID,
material_bom.category,
material_bom.material_type,
material_bom.`size`,
material_bom.description,
material_bom.notes,
(
SELECT sum(material_bom.quanities_ln_ft)
FROM material_bom
WHERE material_bom.job_ID = recvdmaterials.job_ID
AND material_bom.size = recvdmaterials.size
AND material_bom.category = recvdmaterials.category
AND material_bom.description = recvdmaterials.description
AND material_bom.material_type = recvdmaterials.material_type
) AS QtyLn_Ft_Needed,
(
SELECT SUM(recvdmaterials.qty_lnft)
FROM recvdmaterials
WHERE material_bom.job_ID = recvdmaterials.job_ID
AND material_bom.size = recvdmaterials.size
AND material_bom.category = recvdmaterials.category
AND material_bom.description = recvdmaterials.description
) AS QtyLn_Ft_Recvd
FROM material_bom
LEFT OUTER JOIN recvdmaterials ON material_bom.job_ID = 
recvdmaterials.job_ID AND material_bom.category = recvdmaterials.category 
AND material_bom.description = recvdmaterials.description AND 
material_bom.material_type = recvdmaterials.material_type AND 
material_bom.`size` = recvdmaterials.`size`
WHERE (material_bom.job_ID = '20') AND (material_bom.category = 'PIPE')
GROUP BY material_bom.job_ID, material_bom.category, 
material_bom.material_type, material_bom.`size`, material_bom.description
ORDER BY material_bom.category, material_bom.`size`

0 个答案:

没有答案