在MySQL中,如果列A和列B没有相同的值,则获取数据

时间:2017-03-22 01:32:46

标签: mysql

我正在尝试编写一个查询来获取最近登录的客户,但我不确定为什么我没有得到任何结果!

  

lastActivity日期存储在customer表中以及   customer_statistics lastActivity

这是我的问题:

mysql> SELECT `customer`.`lastActivity`, `customer_statistics`.`lastActivity`,  `customer`.`lastActivity` = customer_statistics.lastActivity as todate
FROM (`customer`)
LEFT OUTER JOIN `customer_statistics` ON `customer`.`id` = `customer_statistics`.`customer`
WHERE `customer`.`lastActivity` = `customer_statistics`.`lastActivity` IN ('0', 'NULL')

以下是数据的样子(在何处之前),从这个列表中我只需要第1行和第2行。

+----+------------+------------+-------------+
| id | date 1     | date 2     | todate      |
+----+------------+------------+-------------+
|  1 | 2017-02-12 | 2017-02-13 | 0           |
|  2 | 2017-02-13 | NULL       | NULL        |
|  3 | 2017-02-15 | 2017-02-15 | 1           |
+----+------------+------------+-------------+

1 个答案:

答案 0 :(得分:0)

SELECT null IN (null) from DUAL;返回null。相反,你需要:

SELECT `customer`.`lastActivity`, `customer_statistics`.`lastActivity`,  `customer`.`lastActivity` = customer_statistics.lastActivity as todate
FROM (`customer`)
LEFT OUTER JOIN `customer_statistics` ON `customer`.`id` = `customer_statistics`.`customer`
WHERE `customer`.`lastActivity` != `customer_statistics`.`lastActivity` OR `customer`.`lastActivity` IS NULL OR `customer_statistics`.`lastActivity` IS NULL