结合这两个查询的最佳方法是什么

时间:2018-12-04 14:59:44

标签: mysql sql

当我尝试编写结合这两个查询的联接或联合时,我不断收到mysql错误。 field1A和field1B是我要计算的条件。

SELECT count(*) 
FROM `table1` c1 
    LEFT JOIN `table2` j1 ON (j1.`field1A` = '1') 
WHERE (c1.`field2` = 'Yes' AND c1.`field` is null)")

SELECT count(*) 
FROM `table1` c1 
    LEFT JOIN `table2` j1 ON (j1.`field1B` = '1') 
WHERE (c1.`field2` = 'Yes' AND c1.`field` is null)")

我敢肯定,除了运行两个查询并将总和相加之外,还有更好的方法。

1 个答案:

答案 0 :(得分:1)

您可以通过单选获得它,如下所示。

SELECT count(*) 
   FROM `table1` c1 
        LEFT JOIN `table2` j1 ON (j1.`field1A` = '1')  AND (j1.`field1B` = '1')
    WHERE (c1.`field2` = 'Yes' AND c1.`field` is null)