MySQL行不被忽略

时间:2015-03-29 04:28:50

标签: mysql

我的结果以我想要的方式返回 - 我只想忽略 MPG_VALUE>的行30 nvdtechnical.TECH_Value_Float

但是我的查询会返回所有内容 - 并且 MPG_VALUE 列中不需要的行包含NULL。

如何只返回 MPG_VALUE>行? 30

SELECT *, (SELECT PR_Basic + PR_VAT + PR_Delivery FROM nvdprices 
WHERE PR_Id = capder.cder_ID ORDER BY PR_EffectiveTo ASC LIMIT 1) as P11D,

(SELECT nvdtechnical.TECH_Value_Float FROM nvdtechnical 
 WHERE TECH_TechCode = '11' 
 AND nvdtechnical.TECH_Id = capder.cder_ID
 AND nvdtechnical.TECH_Value_Float > 30 
 LIMIT 1) as MPG_VALUE

from capmod 

JOIN capder ON capder.cder_modcode = capmod.cmod_code 
JOIN caprange ON capder.cder_rancode = caprange.cran_code 
JOIN capman ON caprange.cran_mantextcode = capman.cman_code 
JOIN nvdmodelyear ON capder.cder_ID = MY_Id 

AND nvdmodelyear.MY_EffectiveTo = '0000-00-00 00:00:00' 

JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode

AND nvddictionarycategory.DC_CatCode=4 
AND nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float >= '255' 

JOIN capfueltype ON capfueltype.cft_code = capder.cder_fueltype 
JOIN nvdbodystyle ON capmod.cmod_bodystyle = nvdbodystyle.bs_code 

WHERE caprange.cran_mantextcode='140' 
AND caprange.cran_code='522' 
AND capder.cder_fueltype='P' 
AND capder.cder_transmission='A' 
AND nvdbodystyle.bs_code='3' 
AND (cmod_discontinued=0 OR cmod_discontinued=2015) 
AND capman.cman_code IN ('1','140','164') 
AND caprange.cran_code IN ('924','126','147','955','965','661') 

ORDER BY P11D, capman.cman_name ASC, caprange.cran_name, capmod.cmod_name 

1 个答案:

答案 0 :(得分:1)

这是因为您正在对MPG值进行标量查询。您实际上并未过滤要显示或隐藏的行,而是显示具有与主查询匹配的值的所有行。

想象一下左连接,就像你正在做的那样

SELECT nvdtechnical.TECH_Value_Float FROM nvdtechnical 
 WHERE TECH_TechCode = '11' 
 AND nvdtechnical.TECH_Id = capder.cder_ID
 **AND nvdtechnical.TECH_Value_Float > 30** 
 LIMIT 1

要正确过滤数据,您必须在适用的地方再次过滤:

最简单的方法就是这个。

SELECT *
FROM (
SELECT *, (SELECT PR_Basic + PR_VAT + PR_Delivery FROM nvdprices 
WHERE PR_Id = capder.cder_ID ORDER BY PR_EffectiveTo ASC LIMIT 1) as P11D,

(SELECT nvdtechnical.TECH_Value_Float FROM nvdtechnical 
 WHERE TECH_TechCode = '11' 
 AND nvdtechnical.TECH_Id = capder.cder_ID
 AND nvdtechnical.TECH_Value_Float > 30
 LIMIT 1) as **MPG_VALUE**

from capmod 

JOIN capder ON capder.cder_modcode = capmod.cmod_code 
JOIN caprange ON capder.cder_rancode = caprange.cran_code 
JOIN capman ON caprange.cran_mantextcode = capman.cman_code 
JOIN nvdmodelyear ON capder.cder_ID = MY_Id 

AND nvdmodelyear.MY_EffectiveTo = '0000-00-00 00:00:00' 

JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode

AND nvddictionarycategory.DC_CatCode=4 
AND nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float >= '255' 

JOIN capfueltype ON capfueltype.cft_code = capder.cder_fueltype 
JOIN nvdbodystyle ON capmod.cmod_bodystyle = nvdbodystyle.bs_code 

WHERE caprange.cran_mantextcode='140' 
AND caprange.cran_code='522' 
AND capder.cder_fueltype='P' 
AND capder.cder_transmission='A' 
AND nvdbodystyle.bs_code='3' 
AND (cmod_discontinued=0 OR cmod_discontinued=2015) 
AND capman.cman_code IN ('1','140','164') 
AND caprange.cran_code IN ('924','126','147','955','965','661') 

ORDER BY P11D, capman.cman_name ASC, caprange.cran_name, capmod.cmod_name 
) x
WHERE x.MPG_VALUE IS NOT NULL;

请注意,在外部过滤的x.MPG_VALUE是您的标量查询。

编辑:您可以尝试其他方法,例如内部联接。

SELECT *, (SELECT PR_Basic + PR_VAT + PR_Delivery FROM nvdprices 
WHERE PR_Id = capder.cder_ID ORDER BY PR_EffectiveTo ASC LIMIT 1) as P11D,
MPG_VALUE.TECH_Id
from capmod
JOIN capder ON capder.cder_modcode = capmod.cmod_code 
JOIN caprange ON capder.cder_rancode = caprange.cran_code 
JOIN capman ON caprange.cran_mantextcode = capman.cman_code 
JOIN nvdmodelyear ON capder.cder_ID = MY_Id 
AND nvdmodelyear.MY_EffectiveTo = '0000-00-00 00:00:00' 
JOIN nvdtechnical ON nvdtechnical.TECH_Id = capder.cder_ID
JOIN nvddictionarytechnical ON nvddictionarytechnical.DT_TechCode = nvdtechnical.TECH_TechCode
JOIN nvddictionarycategory ON nvddictionarycategory.DC_CatCode = nvddictionarytechnical.DT_CatCode
AND nvddictionarycategory.DC_CatCode=4 
AND nvddictionarytechnical.DT_TechCode = 67
AND nvdtechnical.TECH_Value_Float >= '255' 
JOIN capfueltype ON capfueltype.cft_code = capder.cder_fueltype 
JOIN nvdbodystyle ON capmod.cmod_bodystyle = nvdbodystyle.bs_code 
INNER JOIN 
(
    SELECT TECH_Value_Float,TECH_Id
    FROM nvdtechnical 
    WHERE TECH_TechCode = '11' 
    AND TECH_Value_Float > 30
) MPG_VALUE
ON MPG_VALUE.TECH_Id = capder.cder_ID
WHERE caprange.cran_mantextcode='140' 
AND caprange.cran_code='522' 
AND capder.cder_fueltype='P' 
AND capder.cder_transmission='A' 
AND nvdbodystyle.bs_code='3' 
AND (cmod_discontinued=0 OR cmod_discontinued=2015) 
AND capman.cman_code IN ('1','140','164') 
AND caprange.cran_code IN ('924','126','147','955','965','661') 
ORDER BY P11D, capman.cman_name ASC, caprange.cran_name, capmod.cmod_name;