SQL更改从列方向到行的选择

时间:2012-04-06 13:14:08

标签: sql

我有一个简单的查询,

select a.*,b.* from myTableA a join myTableB b on a.ID = B.ID

这很好用。但是,它显示为

ID|Value|Fruit|ID1|Value1|Fruit1

如何获得结果

ID|Value|Fruit
1|A|Apple
1|B|Mango

4 个答案:

答案 0 :(得分:0)

select a.* from myTableA a join myTableB b on a.ID = B.ID

答案 1 :(得分:0)

这是您要描述的UNION查询:

SELECT ID, Value, Fruit
FROM myTableA
WHERE ID IN (SELECT B.ID FROM myTableB B)
UNION ALL
SELECT ID, Value, Fruit
FROM myTableB
WHERE ID IN (SELECT A.ID FROM myTableA A)

答案 2 :(得分:0)

select a.ID,
a.Value,
a.Fruit
from myTableA a join myTableB b on a.ID = B.ID

答案 3 :(得分:0)

取消选择b这样

SELECT a.* from myTableA a left join myTableB b on a.ID=b.ID