简体   繁体   English

SQL 查询 where 子句过滤基于另一列值

[英]SQL query where clause filter based on another column value

Below query I am trying to filter status id =7 records for team_id>1 But want to include records with status_id=7 for team_id=1.How can we write the query.下面的查询我正在尝试为 team_id>1 过滤 status_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

A combination of and and or logical operators should do it: 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

Try尝试

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM