0

我在 1 个表中有 2 列,我需要将每列的 COUNT() 组合在一起。但是,如果两列都有值,则该行只需要计算一次。

这是我尝试过但不起作用的方法:

SELECT SUM( IF (table1.col1 IS NOT NULL OR table1.col2 IS NOT NULL, 1, 0) )
FROM table1 INNER JOIN table2 ON table2.col1 = table1.id
    INNER JOIN table3 ON table3.id = table2.personid
WHERE table3.otherid = 1 
    AND table1.col1 > '2000-01-01';*


Sample Data:
Table 1
Col1......Col2
04/17/2013........04/17/2013
null..........04/17/2013
03/26/2013.......02/17/2013
03/25/2013.........null
null.........null
02/22/2013...........02/21/2013

结果应该是 = 5。因为 5 条记录在任一列中至少有 1 个日期,但在两列中都有日期的记录只计算一次

4

1 回答 1

0

弄清楚了。

SELECT COUNT(*)
FROM table1 INNER JOIN table2 ON table2.col1 = table1.id
    INNER JOIN table2 ON table3.id = table2.personid
WHERE table3.otherid = 1 
    AND (table1.col1 > '2000-01-01' OR table1.col2 > '2000-01-01');
于 2013-09-26T15:38:11.383 回答