A 表
id money post_time
1 10 1449976443
2 30 1449976455
B表
id money post_time
1 5 1449976243
想要查出的结果如:
a_table_money b_table_money post_time
10 0 1449976443
30 0 1449976455
0 5 1449976243
请求朋友帮助,怎么写 SQL
id money post_time
1 10 1449976443
2 30 1449976455
B表
id money post_time
1 5 1449976243
想要查出的结果如:
a_table_money b_table_money post_time
10 0 1449976443
30 0 1449976455
0 5 1449976243
请求朋友帮助,怎么写 SQL
解决方案
10
select money as a_table_money, 0 as b_table_money ,post_time from A表
union all
select 0 as a_table_money, money as b_table_money ,post_time from B表
union all
select 0 as a_table_money, money as b_table_money ,post_time from B表
10
楼上,假如两个表的post_time相同,查出来的数据,应该不是LZ想要的
10
在2楼的基础上,再加个group by,避免楼上说的post_time相同
10
LZ你好
3楼正解,代码如下
select money as a_table_money, 0 as b_table_money ,post_time from A表
union all
select 0 as a_table_money, money as b_table_money ,post_time from B表
group by post_time
建议LZ一试
3楼正解,代码如下
select money as a_table_money, 0 as b_table_money ,post_time from A表
union all
select 0 as a_table_money, money as b_table_money ,post_time from B表
group by post_time
建议LZ一试