select table1.项目,sum(table1.数量1),sum(table2.数量2) from table1,table2 where table1.项目 = table2.项目 group by table1.项目
select t1.c1, sum(t1.c2) as count1, sum(t2.c2) as count2 from t1 right join t2 on t1.c1 = t2.c1 group by t1.c1
嗯,无兄的考虑不周全,可能A里有的项目,B里没有 应该是把所有的项目筛选出来 Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a 然后用子查询 select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目), 数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from (Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a ) b
table2里有的项目 table1里可能没有
用一条SQl能解决吗,要效率。
嗯,无兄的考虑不周全,可能A里有的项目,B里没有 应该是把所有的项目筛选出来 Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a 然后用子查询 select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目), 数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from (Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a ) b 嗯,无兄的考虑不周全,可能A里有的项目,B里没有 应该是把所有的项目筛选出来 Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a 然后用子查询 select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目), 数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from (Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a ) b 嗯,无兄的考虑不周全,可能A里有的项目,B里没有 应该是把所有的项目筛选出来 Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a 然后用子查询 select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目), 数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from (Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a ) b 嗯,无兄的考虑不周全,可能A里有的项目,B里没有 应该是把所有的项目筛选出来 Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a 然后用子查询 select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目), 数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from (Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a ) b 嗯,无兄的考虑不周全,可能A里有的项目,B里没有 应该是把所有的项目筛选出来 Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a 然后用子查询 select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目), 数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from (Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a ) b 嗯,无兄的考虑不周全,可能A里有的项目,B里没有 应该是把所有的项目筛选出来 Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a 然后用子查询 select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目), 数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from (Select Distinct 项目 from ( Select table1.项目 as 项目 from table1 union all Select table2.项目 from table2) a ) b
from table1,table2
where table1.项目 = table2.项目
group by table1.项目
应该是把所有的项目筛选出来
Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
然后用子查询
select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from
(Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
)
b
应该是把所有的项目筛选出来
Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
然后用子查询
select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from
(Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
)
b
嗯,无兄的考虑不周全,可能A里有的项目,B里没有
应该是把所有的项目筛选出来
Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
然后用子查询
select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from
(Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
)
b
嗯,无兄的考虑不周全,可能A里有的项目,B里没有
应该是把所有的项目筛选出来
Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
然后用子查询
select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from
(Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
)
b
嗯,无兄的考虑不周全,可能A里有的项目,B里没有
应该是把所有的项目筛选出来
Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
然后用子查询
select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from
(Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
)
b
嗯,无兄的考虑不周全,可能A里有的项目,B里没有
应该是把所有的项目筛选出来
Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
然后用子查询
select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from
(Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
)
b
嗯,无兄的考虑不周全,可能A里有的项目,B里没有
应该是把所有的项目筛选出来
Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
然后用子查询
select 项目,数量1=(Select sum(table1.数量) from table1 where 项目=b.项目),
数量2=(Select sum(table2.数量) from table2 where 项目=b.项目) from
(Select Distinct 项目 from (
Select table1.项目 as 项目 from table1
union all
Select table2.项目 from table2)
a
)
b