日期:2014-05-16  浏览次数:20695 次

求一个稍微复杂的sql语句
有一些人,分别在一些组中,有的人可能在两个组中。每人有一篇文章,我怎么根据随意的一个找到他所在的组的所有文章。

例如:A   B   C   D   四个人   X   Y   Z   三个组
A=> X   B=> X/Z   C=> Y   D=> Z   (人=> 组,例如B=> X/Z   意思是B属于   X   和Z   两个组)

例如现在根据B   我怎么找到B所在的组的所有文章。

数据库表1是每个人和组的对照表,数据库表2是每个人的文章(表2中没有组的信息)
table1
person   class
table2
person   article

这样的sql语句应该怎么写?

------解决方案--------------------
select table1.persion,table2.article
from table1 innser join table2
on table1.persion=table2.persion
where table1.class= 'Z '
------解决方案--------------------
select article from table2 where table2.person in
(select DISTINCT person from table1 where class in
( select class from table1 where person = 'B ') )