我有表A 格式如下 | id | value | | 1 | A | | 2 | A | | 3 | A |表B id | value | | 5 | B | | 6 | B | | 4 | B | | 1 | B |表A与B连接 怎样写sql才能形成如下表结构 id value 1 A 2 A 3 A 4 B 5 B 6 B
select * from a union select * from b 不行啊 --+-------+ | id | value | +----+-------+ | 1 | A | | 4 | B | | 2 | A | | 6 | B | | 1 | B | | 5 | B | | 3 | A | id 1 有两个
select * from a union select * from b where id not in (select id from a)
select * from ( select * from a union select * from b where id not in (select id from a) ) aa order by id asc
union
select * from b
union
select * from b 不行啊
--+-------+
| id | value |
+----+-------+
| 1 | A |
| 4 | B |
| 2 | A |
| 6 | B |
| 1 | B |
| 5 | B |
| 3 | A | id 1 有两个
union
select * from b where id not in (select id from a)
*
from (
select * from a
union
select * from b where id not in (select id from a)
) aa
order by id asc