开发者

SQL query giving wrong result

开发者 https://www.devze.com 2023-03-05 13:18 出处:网络
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.

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`


I have a feeling the conditions are being evalulated like this

status_change_date or (created_date and status.id = 2)

You might try using parentheses to designate the grouping of your conditions

( DATE(items.status_change_date)... OR DATE(items.created_date)... )
    AND status.id = 2


Your WHERE clause looks wrong, try:

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


Put brackets around your date clauses:

  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 
0

精彩评论

暂无评论...
验证码 换一张
取 消