SQL 查询给出错误结果
当我运行以下查询时,它仅显示 1 行。基于“created_date”条件,就好像我删除“created_date”条件并只保留“status_change_date”条件一样,它显示 3 行。
我想查看在指定时间内创建或修改的所有项目。我无法找到查询中有什么问题
`SELECT DISTINCT products.id AS product, status.id AS stat, COUNT(*) AS total FROM items INNER JOIN products ON (items.fk_product_id = products.id) INNER JOIN status ON (items.fk_status = status.id) WHERE DATE(items.status_change_date) BETWEEN '2011-04-13' AND '2011-05-13' OR DATE(items.created_date) BETWEEN '2011-04-13' AND '2011-05-13' AND status.id = 2 GROUP BY products.name, status.name order by products.name`
When i run following query it shows only 1 row. Based on "created_date" condition, where as if i remove "created_date" condition and just keep "status_change_date" condition it shows 3 rows.
I want to see all items those were created or modified in specified time. I am unable to find whats wrong in the query
`SELECT DISTINCT products.id AS product, status.id AS stat, COUNT(*) AS total FROM items INNER JOIN products ON (items.fk_product_id = products.id) INNER JOIN status ON (items.fk_status = status.id) WHERE DATE(items.status_change_date) BETWEEN '2011-04-13' AND '2011-05-13' OR DATE(items.created_date) BETWEEN '2011-04-13' AND '2011-05-13' AND status.id = 2 GROUP BY products.name, status.name order by products.name`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一种感觉,条件正在这样评估
您可以尝试使用括号来指定条件分组
I have a feeling the conditions are being evalulated like this
You might try using parentheses to designate the grouping of your conditions
您的 WHERE 子句看起来错误,请尝试:
Your WHERE clause looks wrong, try:
将日期条款放在括号中:
Put brackets around your date clauses: