SQL怎么同时查询两个表的数据?

2024-11-15 05:58:26
推荐回答(5个)
回答1:

你的题目写的有点不清楚,有问题再补充,参考以下代码

SELECT COUNT(AAB001)  AS 不同值的数量 FROM(
SELECT AAB001 FROM AC01
UNION
SELECT AAB001 FROM AZ03 ) AS TEMP

回答2:

同时输出AC01表中AAB004和AZ03表中AAB001和AAB002的数据
select a.AAB004, b.AAB001, b.AAB002 from AC01 a, AZ03 b;
(可以加where条件,例如:where a.AAB001=b.AAB001 )。
“两个表中有相同的字段AAB001,然后我需要统计他们AAB001不同值的数量该怎么写”
是不是要统计出 AC01表中AAB001与AZ03表中AAB001不同值的个数呀?
select a.AAB004, b.AAB001, b.AAB002, count(*) as numb
from AC01 a, AZ03 b
where a.AAB001!=b.AAB001 ;

回答3:

select AC01.AAB004,AZ03.AAB001,AZ03.AAB002,sum(case when AC01,AZ03 where AC01.AAB001!=AZ03.AAB001 then 1 else 0) from AC01,AZ03 where AC01.AAB001=AZ03.AAB001)

回答4:

select t1.AAB004, t2.AAB001,t2.AAB002 from AC01 t1,AZ03 t2 where t1.AAB001=t2.AAB001

回答5:

这样可以完成统计毕竟复杂,用到了效率不高的子查询,绝对会有更好的办法,抛砖引玉,自勉
select ac.AAB004,az.AAB001,az.AAB002,
(((select count(AAB001) from AC01)+
(select count(AAB001) from AZ03)))-
(select count(ac.AAB001) from AC01 as ac inner join AZ03 as az
on ac.AAB001=az.AAB001)*2 as 不同的数量
from AC01 as ac inner join AZ03 as az
on ac.AAB001=az.AAB001