我有一张桌子最难
One Two
apple orange
orange grapes
banana strawberry
orange strawberry
grapes apple
现在你可以看到我在这里列出了5对。 让我告诉你,它不是一个普通的表,因为在这个表中,水果的名称(苹果)有时在第一列,有时在第二列。
And another table Description
name color
apple red
orange orange
strawberry pink
banana yellow
grapes black
假设iam banana,我不想看到与我配对的水果的描述。 现在有人可以告诉我如何在mysql中显示与以下所有内容配对的水果的详细信息,但不包括香蕉:草莓,葡萄,苹果。
我告诉他们这样做的方法,这在mysql中是不受支持的。
Select * from description
natural join (select one as name where two='apple' union select two as name where one='apple') as t1
natural join (select one as name where two='grapes' union select two as name where one='grapes') as t2
natural join (select one as name where two='strawberry' union select two as name where one='strawberry') as t3
MINUS
Select * from description
natural join (select one as name where two='banana' union select two as name where one='banana') as b6
答案 0 :(得分:3)
SELECT * FROM Description
NATURAL JOIN (
SELECT One AS name FROM hardest WHERE Two = 'strawberry'
UNION
SELECT Two AS name FROM hardest WHERE One = 'strawberry'
) AS t1
NATURAL JOIN (
SELECT One AS name FROM hardest WHERE Two = 'grapes'
UNION
SELECT Two AS name FROM hardest WHERE One = 'grapes'
) AS t2
NATURAL JOIN (
SELECT One AS name FROM hardest WHERE Two = 'apple'
UNION
SELECT Two AS name FROM hardest WHERE One = 'apple'
) AS t3;
<强>更新强>
OP更新了问题,添加了结果集应排除与banana
配对的任何水果的约束;然后,他使用WHERE name NOT IN ( SELECT ... )
提出了一个解决方案,但质疑外部联接是否会更有效。
首先,使用外部联接的查询:
SELECT Description.* FROM Description
NATURAL JOIN (
SELECT One AS name FROM hardest WHERE Two = 'strawberry'
UNION
SELECT Two AS name FROM hardest WHERE One = 'strawberry'
) AS t1
NATURAL JOIN (
SELECT One AS name FROM hardest WHERE Two = 'grapes'
UNION
SELECT Two AS name FROM hardest WHERE One = 'grapes'
) AS t2
NATURAL JOIN (
SELECT One AS name FROM hardest WHERE Two = 'apple'
UNION
SELECT Two AS name FROM hardest WHERE One = 'apple'
) AS t3
NATURAL LEFT JOIN (
SELECT One AS name, TRUE As hasBanana FROM hardest WHERE Two = 'banana'
UNION
SELECT Two AS name, TRUE As hasBanana FROM hardest WHERE One = 'banana'
) AS t4
WHERE hasBanana IS NULL;
关于哪个更有效,this article比较了不同的方法。它陈述(关于[NOT] IN ( SELECT ... )
:
基本上,这与
LEFT JOIN
/IS NULL
使用的计划完全相同,尽管这些计划由不同的代码分支执行,但它们在{{1}的结果中看起来不同}。实际上算法实际上是相同的,并且查询同时完成。
答案 1 :(得分:0)
我正在清理我的答案,因为问题正在改变原来的问题。
答案 2 :(得分:0)
您好你已经回答了自己的问题,你必须使用IN
代替=
。请检查以下查询...
select * from hardest,description where
(one=name and two IN ('apple' , 'strawberry','grapes'))
or
(one IN ('apple','strawberry' , 'grapes') and two=name)
感谢
答案 3 :(得分:0)
请确保我写作的方式效率最高。如果您有其他方式更有效,请写下
SELECT * FROM Description
NATURAL JOIN ( SELECT One AS name FROM hardest WHERE Two = 'strawberry'
UNION SELECT Two AS name FROM hardest WHERE One = 'strawberry' ) AS t1
NATURAL JOIN ( SELECT One AS name FROM hardest WHERE Two = 'grapes'
UNION SELECT Two AS name FROM hardest WHERE One = 'grapes' ) AS t2
NATURAL JOIN ( SELECT One AS name FROM hardest WHERE Two = 'apples'
UNION SELECT Two AS name FROM hardest WHERE One = 'apples' ) AS t3
where name not in ( SELECT One AS name FROM hardest WHERE Two = 'banana'
UNION SELECT Two AS name FROM hardest WHERE One = 'banana') AS fz );