也许这样会更有意义。
MySQL“表”
代码:
id1 | id2
1 | 2
1 | 3
2 | 4
2 | 3
3 | 4
3 | 5在id1 = 1中,这个id连接到2和3:1->2,1->3
我想要做的是输出2和3的ID,它们没有连接到1,在这种情况下,这些ID将是4和5。
2->4 (1没有连接到4= OK)2->3 (1连接到3= NOT OK)3->4 (1没有连接到4= OK) ...but )不应该显示两次,只因为2和3连接到4!!3->5 (1没有连接到5= OK)我能想出的唯一方法看起来类似于下面的OHO代码,但如果可能的话,我只想在一个简单的MySQL查询中完成这一切(即加入?):
$a = mysql_query("SELECT id2 FROM table WHERE id1 = 1");
while($b = mysql_fetch_assoc($a))
{
$c = mysql_query("SELECT id2 FROM table WHERE id1 = $b[id2]");
while($d = mysql_fetch_assoc($c))
{
$e = mysql_query("SELECT id2 FROM table WHERE id1 = 1 AND id2 = $d[id2]");
$f = mysql_fetch_assoc($e);
if(!$f['id2'])
{
echo $f['id2'];
}
}
}注意:上面的代码的一个问题是它会输出4次,因为2和3都连接到4。
发布于 2010-08-06 19:10:48
那这个呢?
select distinct a.id2
from myTable a
join (select id1, id2 from myTable where id1 = 1) b on a.id1 = b.id2
where NOT EXISTS (select 1 from myTable where id1 = b.id1)发布于 2010-08-06 19:34:11
那SELECT DISTINCT id2 FROM table WHERE id1 != 1呢
发布于 2010-08-06 19:34:34
我可能不太理解你的问题,但你想要这样的东西吗?
select
distinct a.id2
from
myTable a
where
a.id2 not in (select id2 from myTable where id1 = 1);https://stackoverflow.com/questions/3426897
复制相似问题