我正在一个数据库上可以synthetized像:如何从数据库中选择一组特定元素?
TAB1: NICK, COUNTRY
TAB2: PLAYER, FRIEND
在TAB2,播放器和朋友都可以连接到各自的一个TAB1.NICK; NICK是主键,PLAYER和FRIEND都是外键。
我需要找到多少玩家朋友只与来自同一个国家的人,但一对夫妇下午,我依然不能管理做到这一点之后......
我想的最后一件事:
select count(*) from TAB1 as p1
join TAB2 as f1 on p1.nick = f1.player
join TAB1 as p2 on f1.friend = p2.nick
where p1.country is not null and p2.country is not null
and p1.country = p2.country
and not exists (select * from TAB1 as p0
join TAB2 as f0 on p0.nick = f0.nick
join TAB1 as p3 on f0.friend = p3.nickname
where p0.country is not null and p3.country is not null
and p0.country <> p3.country)
没有PK/FK在这里? – hsz 2011-04-19 15:47:19
请问数据库模式? (特别关系) – 2011-04-19 15:48:52
编辑,遗憾的东西。 – Gurzo 2011-04-19 15:50:14