SQL query where clause filter based on another column value 查询条件

SQL query where clause filter based on another column value

下面的查询我正在尝试为 team_id>1 过滤状态 ID =7 的记录 但是想要为 team_id=1.How 包含 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

尝试

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

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

brace location can change the result set.

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