T1 table
+-----------+------------------+
| cookie_id | impression |
+-----------+------------------+
| 123 | 6/17/15 |
| 123 | 6/18/15 |
| 123 | 6/18/15 |
| 234 | 6/20/15 |
| 234 | 6/22/15 |
+-----------+------------------+
T2 table
+-----------+---------+---------+---------+
| cookie_id | HP | search | book |
+-----------+---------+---------+---------+
| 123 | 6/17/15 | | |
| 123 | 6/18/15 | | |
| 123 | | 6/18/15 | |
| 123 | | | 6/19/15 |
| 234 | 6/23/15 | | |
| 234 | | 6/25/15 | |
| 234 | | | 6/29/15 |
+-----------+---------+---------+---------+
我们希望将T1和T2加入预期结果,如下表T3所示:
+-----------+------------+---------+---------+---------+
| cookie_id | impression | HP | search | book |
+-----------+------------+---------+---------+---------+
| 123 | 6/17/15 | | | |
| 123 | 6/18/15 | | | |
| 123 | 6/18/15 | | | |
| 123 | | 6/17/15 | | |
| 123 | | 6/18/15 | | |
| 123 | | | 6/18/15 | |
| 123 | | | | 6/19/15 |
| 234 | 6/20/15 | | | |
| 234 | 6/22/15 | | | |
| 234 | | 6/23/15 | | |
| 234 | | | 6/25/15 | |
| 234 | | | | 6/29/15 |
+-----------+------------+---------+---------+---------+
答案 0 :(得分:1)
http://sqlfiddle.com/#!9/375e6/2
SELECT cookie_id, impression, null,null, null
FROM t1
UNION ALL
SELECT cookie_id, null as impression, hp, search,book
FROM t2
答案 1 :(得分:0)
正如评论中所提到的,你似乎想要UNION这些表:这样做会,但它可能不是你真正需要的......
SELECT * FROM (
(SELECT cookie_id,
"" AS impression,
HP,
search,
book
FROM T1)
UNION
(SELECT cookie_id,
impression,
"" AS HP,
"" AS search,
"" AS book
FROM T2)
) a
ORDER BY cookie_id;
这不是我的想法,也许我写错了什么的。