我有一张如下表所示的表格:
示例:
表:
CREATE TABLE testo
(
namea VARCHAR(10),
nameb VARCHAR(10),
details VARCHAR(10)
)
有一些记录:
INSERT INTO testo VALUES('A','C','xyz');
INSERT INTO testo VALUES('A','B','oxo');
INSERT INTO testo VALUES('B','D','ner');
INSERT INTO testo VALUES('X','Y','tye');
INSERT INTO testo VALUES('Z','U','txt');
INSERT INTO testo VALUES('Y','N','str');
namea nameb details
-------------------
A C xyz
A B oxo
B D ner
X Y tye
Z U txt
Y N str
注意:我想在nameb
中显示namea
存在的记录,并希望显示namea
所属的记录nameb
在namea
中出现。
预期结果:
namea nameb details
---------------------
A B oxo
B D ner
X Y tye
Y N str
我曾尝试过:
select * from testo
where nameb in (select namea from testo);
但只得到:
namea nameb details
---------------------
B D ner
Y N str
SQL小提琴: - > http://sqlfiddle.com/#!2/0a3bb2/1
答案 0 :(得分:1)
select *
from testo
where
nameb in (select namea from testo)
or namea in (select nameb from testo)
答案 1 :(得分:0)
试试这个: SELECT * FROM testo WHERE namea IN(SELECT name FROM testo)union SELECT * FROM testo 名称为IN(SELECT namea FROM testo);