2013-03-23 39 views
0

在知道兩個或更多具有相同值的單元時有問題。 例如: 列包含作者的名稱和另一列包含城市的生活.. 正試圖知道作家居住在同一個城市的名字.. 所以我想檢查城市列以瞭解是否有同一個城市或沒有,但我不知道語法.. :)如何檢查數據庫系統sql server中同一列中的單元格是否具有相同的值?

select authors.au_fname , authors.au_lname ,city -- to present the first & last name of author 
from authors 
Where -- I don't know the condition here 
+0

你期望輸出什麼?你能給個例子嗎? – Geier 2013-03-23 18:14:42

+0

列作者包含姓名 列城包含他們所居住的城市 還有誰住在同一城市的作者 所以我希望得到這些作者與他們生活在 – SUE 2013-03-23 18:22:32

+0

普通城市,不是一個實例的名稱,但我認爲Hélio有你的答案。 – Geier 2013-03-23 18:24:34

回答

1
select a1.au_fname , a1.au_lname, a2.au_fname , a2.au_lname, city  
from authors a1 inner join authors a2 on a1.id <> a2.id 
where a1.city= a2.city 

說明:

一個有兩個作家的每個組合進行比較,因此表中加入本身在不同的主要科目上ys(我假設authors.id)。最後,where指出,只有生活在同一城市的作者纔會被輸出,從而篩選出不在同一城市的作者巴黎。

0

我覺得你只是想通過城市進行排序:

select authors.au_fname , authors.au_lname ,city -- to present the first & last name of author 
from authors 
order by city 

如果你只是想選擇有2名或更多的作家城市,嗯,這裏是在任何數據庫中運行的一種方法:

select authors.au_fname , authors.au_lname ,city -- to present the first & last name of author 
from authors 
where city in (select city from authors group by city having count(*) >= 2) 
order by city 
相關問題