我有一些代码可以从mysql中获取一组结果,请参阅下面的内容,
SELECT string, COUNT(string) as myCount
FROM ip_stats WHERE string != '' OR string IS NOT NULL
GROUP BY string
ORDER BY myCount DESC
LIMIT 10
现在这段代码完美无缺,但是当我在下面添加另一个where子句时,它会返回相同的结果,并且似乎完全无视where where claus的member =部分?
SELECT string, COUNT(string) as myCount
FROM ip_stats WHERE member_id = '000826' string != '' OR string IS NOT NULL
GROUP BY string
ORDER BY myCount DESC
LIMIT 10
任何想法或建议表示赞赏。
答案 0 :(得分:2)
你的where子句有一些错误:
WHERE member_id = '000826' string != '' OR string IS NOT NULL
应该是
WHERE member_id = '000826' AND (string != '' OR string IS NOT NULL)
答案 1 :(得分:0)
AND-part has priority over the OR-part。放括号来解决这个问题:
SELECT string, COUNT(string) as myCount
FROM ip_stats
WHERE member_id = '000826'
AND (string != '' OR string IS NOT NULL)
GROUP BY string
ORDER BY myCount DESC
LIMIT 10
你写的东西实际上给了这个,这不是你想要的:
WHERE (member_id = '000826' AND string != '')
OR string IS NOT NULL
答案 2 :(得分:0)
这没有意义,所以我认为这可能不是你想要的:
WHERE string != '' OR string IS NOT NULL
string IS NOT NULL
将返回所有非空值(包括'')。
我猜你要排除空字符串 和 空字符串值,在这种情况下,这可能是你想要的查询:
SELECT string, COUNT(string) as myCount
FROM ip_stats
WHERE member_id = '000826'
AND string IS NOT NULL
AND string != ''
GROUP BY string
ORDER BY myCount DESC
LIMIT 10