查询特定字段的元素不为空的行

时间:2017-12-30 18:18:26

标签: mysql

我有以下MySQL查询

SELECT field1,
COUNT(DISTINCT field2) AS numbercount, 
MAX(ADDTIME(`date`,`start`)) AS `datetime` FROM mydatabase GROUP BY field1 
ORDER BY numbercount DESC, `datetime` ASC LIMIT 0,11

但我的结果并不像我想要的那样。 我只需要考虑" field1"有任何价值,不是空的。

建议?

编辑: 同时我测试了以下解决方案(添加不喜欢'')并且似乎没问题:

SELECT field1, COUNT(DISTINCT field2) AS numbercount,
MAX(ADDTIME(date,start)) AS datetime FROM mydatabase WHERE NOT LIKE '' GROUP BY field1
ORDER BY numbercount DESC, datetime ASC LIMIT 0,11

有更好的解决方案吗?

1 个答案:

答案 0 :(得分:2)

您需要添加字段不为空和空语句。

SELECT field1,
COUNT(DISTINCT field2) AS numbercount, 
MAX(ADDTIME(`date`,`start`)) AS `datetime`
FROM mydatabase WHERE field1<>null AND TRIM(field1) <> ‘’ GROUP BY field1 
ORDER BY numbercount DESC, `datetime` ASC LIMIT 0,11