注意,两张表的字段名最好保持相同
查询两张表的数据,包含相同的数据:
select id from tab_a where a='1'
union ALL
select id from tab_b where a='1'
order by time desc limit 0,10
查询两张表的数据,去掉相同的数据:
select id from tab_a where a='1'
union
select id from tab_b where a='1'
order by time desc limit 0,10
查询两张表合并后的总条数:
SELECT SUM(aa) from(
select COUNT(*) as aa
from tab_a where id='2'
union ALL
select COUNT(*) as aa
from tab_b
where id='2' )tab_c
本文介绍如何使用SQL的联合查询来获取两张表中的数据,并通过不同的UNION操作展示如何保留或去除重复项。此外,还提供了计算两张表合并后总记录数的方法。
1672

被折叠的 条评论
为什么被折叠?



