2013-03-23 80 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 
相关问题