簡體   English   中英

WHERE 子句中同一列上的多個條件

[英]Multiple conditions on the same column in the WHERE clause

我有一張像這樣的桌子——

RecordID   PropertyID       PropertyVal
--------------------------------------------------
3215            7           john doe
3215            11          Chicago
3215            13          Business Development Analyst
3216            7           jane doe
3216            11          Chicago
3216            13          Managing Director
3217            7           mike smith
3217            11          Chicago
3217            13          Business Development Analyst
3218            7           john smith
3218            11          Seattle
3218            13          Managing Director

如何返回PropertyID = 13 AND PropertyVal='Business Development Analyst'AND PropertyID = 11 AND PropertyVal = 'Chicago'的用戶姓名。 如何為同一列執行多個 where 子句?

編輯:我需要結果集看起來像這樣 -

Name
----
John Doe
Mike Smith
select PropertyVal
from your_table
where PropertyID = 7
and RecordID in 
(
  select RecordID   
  from your_table
  where (PropertyID = 13 AND PropertyVal='Business Development Analyst')
     or (PropertyID = 11 AND PropertyVal = 'Chicago')
  group by RecordID   
  having count(distinct PropertyID) = 2
)

不確定你到底想要什么。 這可能是

...
where (PropertyID = 13 AND PropertyVal='Business Development Analyst')
   or (PropertyID = 11 AND PropertyVal = 'Chicago')

要么

...
where PropertyID in (13, 11) 
and PropertyVal in ('Business Development Analyst', 'Chicago')

我們可以使用 JOIN 子句...

前任:

 SELECT LIST_OF_COLUMNS FROM TBL1 T1 JOIN TBL2 T2
    ON T1.COL1=T2.COL1 AND 
    T1.COL2=T2.COL2 AND 
    T1.COL3=T2.COL3 AND 
    T1.COL4=T2.COL4 
SELECT LIST_OF_COLUMNS FROM TBL1 T1 JOIN TBL2 T2
    ON T1.COL1=T2.COL1 AND 
    T1.COL2=T2.COL2 AND 
    T1.COL3=T2.COL3 AND 
    T1.COL4=T2.COL4 

使用 INTERSECT 運算符

select PropertyVal as "Name"
from mytable
where PropertyID=7
and RecordID in 
(
    select RecordID
    from mytable
    where PropertyID = 13 AND PropertyVal='Business Development Analyst'

    intersect

    select RecordID
    from mytable
    where PropertyID = 11 AND PropertyVal='Chicago'

)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM