SQL查询where子句根据另一列值进行过滤

时间:2015-03-11 07:04:42

标签: sql sql-server oracle select

以下查询我正在尝试过滤team_id> 1的状态ID = 7记录 但是想要为team_id = 1包含status_id = 7的记录。我们如何编写查询。

 SELECT MAX(created_date) AS maxtime,
        TEAM_ID,
        ticket_id 
 FROM ri_ticket_status_history  
 WHERE status_id<>7    
 GROUP BY TEAM_ID,ticket_id

3 个答案:

答案 0 :(得分:2)

andor逻辑运算符的组合应该这样做:

SELECT   MAX(created_date) AS maxtime ,team_id, ticket_id 
FROM     ri_ticket_status_history 
WHERE    (status_id<>7 AND team_id > 1) OR team_id = 1
GROUP BY team_id, ticket_id

答案 1 :(得分:1)

尝试

select max(created_date) as maxtime ,TEAM_ID,ticket_id 
from  ri_ticket_status_history 
where status_id<>7  or  (status_id=7 and team_id=1)
group by TEAM_ID,ticket_id

答案 2 :(得分:1)

  

大括号位置可以改变结果集。

select max(created_date) as maxtime ,TEAM_ID,ticket_id 
from  ri_ticket_status_history 
where ( status_id<>7  or  (status_id=7 and team_id=1))
group by TEAM_ID,ticket_id