我有两个疑问。首先将返回10行,第二次查询将返回2行。但是,当我将查询合并为一个时,它仍然只返回10行。
以下是我的疑问。
SELECT *
FROM contentmsg
WHERE apmc_id = '5284a327e4b0bc1eaf3d118e'
AND commodity_id IS NULL
AND lang_id IS NULL
AND date < '2014-07-05 09:25'
AND date >= '2014-06-13 14:17:22'
ORDER BY -date
LIMIT 0, 10
输出 两条记录
SELECT *
FROM contentmsg
WHERE apmc_id = '5284a327e4b0bc1eaf3d118e'
AND commodity_id IN ( '5226d086e4b05917e5ca33bc', '5226d086e4b05917e5ca33b6', '5226d086e4b05917e5ca33d9', '5226d086e4b05917e5ca3408',
'5226d086e4b05917e5ca33e1', '5226d086e4b05917e5ca33ec', '5226d086e4b05917e5ca33b4' )
AND lang_id = '51fb5a2ee4b0e18f7f89ecc8'
AND date < '2014-07-05 09:25'
AND date >= '2014-06-13 14:17:22'
ORDER BY date
LIMIT 0, 10
输出 10条记录..
这些查询之间的唯一区别是,goods_id设置为null,并且在第一个查询中将lang_id设置为null。
我已尝试使用此查询,其中添加了“OR”
SELECT *
FROM contentmsg
WHERE apmc_id = '5284a327e4b0bc1eaf3d118e'
AND commodity_id IS NULL
OR commodity_id IN ( '5226d086e4b05917e5ca33bc', '5226d086e4b05917e5ca33b6', '5226d086e4b05917e5ca33d9', '5226d086e4b05917e5ca3408',
'5226d086e4b05917e5ca33e1', '5226d086e4b05917e5ca33ec', '5226d086e4b05917e5ca33b4' )
AND lang_id IS NULL
OR lang_id = '51fb5a2ee4b0e18f7f89ecc8'
AND date < '2014-07-05 09:25'
AND date >= '2014-06-13 14:17:22'
ORDER BY -date
LIMIT 0, 10
查询中有错误吗?
答案 0 :(得分:2)
您需要注意order of precedence与AND
&amp;在OR
之前评估AND
个运算符(OR
)。要解决此问题,请将操作括在括号中:
SELECT *
FROM contentmsg
WHERE apmc_id = '5284a327e4b0bc1eaf3d118e'
AND ( commodity_id IN ( '5226d086e4b05917e5ca33bc', '5226d086e4b05917e5ca33b6', '5226d086e4b05917e5ca33d9', '5226d086e4b05917e5ca3408',
'5226d086e4b05917e5ca33e1', '5226d086e4b05917e5ca33ec', '5226d086e4b05917e5ca33b4' )
AND lang_id = '51fb5a2ee4b0e18f7f89ecc8' )
OR ( commodity_id IS NULL
AND lang_id IS NULL )
AND date < '2014-07-05 09:25'
AND date >= '2014-06-13 14:17:22'
ORDER BY -date