mysql合并表查询的问题,求帮助

MySql 码拜 8年前 (2016-02-15) 942次浏览
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
解决方案

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表

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一试

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明mysql合并表查询的问题,求帮助
喜欢 (0)
[1034331897@qq.com]
分享 (0)