SQL两个表合并查询的问题 比如说,有A表和B表。两个表结构都一样,A表保存2个月内的数据,B表保存2个月前的数据,现在我想以A表字段1为条件查询,也就是说再A表中没有就在B表中查,请教各位高手,SQl语句应该怎么写。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select * from a where col1='aa'union allselect * from b where col1='aa' [code=SQL]select * from (select * from a union select * from b ) Awhere A.col1 =xxx[/code select * from (select * from a union allselect * from b) tbwhere a1>0 如果B表数据量不大就用union all同时查,数据量大就先判断A表是否有返回值再查询B表 if exist (select * from A where 条件) begin 查询A表 endelse begin 查询B表 end这样执行效率会比用union好些 sql语句优化问题. 快速结贴. mssql数据库csv文件如何导入数据啊 请教一个sql语句 关于 “商品 和 商品明细 ”的 写代码的规范就是,关键字全部大写,表名则要小写,可是频频更换大小写,太麻烦了.所以问问大家,有窍门吗? 求一个表中成绩最差的同学名字 因为学习需要,请推荐几个好的 国外sql server 和oracle的论坛 怎样打开*.db的文件~!? 如何用sql查询实现下面两张报表的合并? 请高手指点 SQL SERVER 数组 一个困扰我很久的问题 酒店管理系统的数据库关系、约束、外键、索引、存储过程、触发器、权限、视图等的设定
union all
select * from b where col1='aa'
select * from
(select * from a
union
select * from b ) A
where A.col1 =xxx[/code
select * from
(select * from a
union all
select * from b) tb
where a1>0
if exist (select * from A where 条件)
begin
查询A表
end
else
begin
查询B表
end这样执行效率会比用union好些