改一下 select t2.id,t2.name,total from (select count(*) total from t1,t2 where t1.id = t2.id),t2;
我交换了他们的次序,结果还是一样的 select count(*) total from t1,t2 where t1.id = t2.id total = 5 select count(*) total from t1,t2 where t1.id = t2.id where t2.id =1 total = 2 select count(*) total from t1,t2 where t1.id = t2.id where t2.id =2 total = 3select t2.id,t2.name,total from (select count(*) total from t1,t2 where t1.id = t2.id),t2;id name total 1 a 5 2 b 5我需要 id name total 1 a 2 2 b 3
select t2.id,t2.name,sum(total) from (select count(*) total from t1,t2 where t1.id = t2.id),t2;
select t2.id,t2.name,t0.total from t2,(select count(*) total,t2.id from t2,t1 where t1.id=t2.id group by t2.id) t0 where t2.id=t0.id;
select t2.id,t2.name,t2.id+1 total from t2
To black_dragon(半仙) 只得到Total不为0时的记录;To bzszp(SongZip) select t2.id,t2.name,sum(total) from (select count(*) total from t1,t2 where t1.id = t2.id),t2 group by t2.id,t2.name 结果仍为 id name total 1 a 5 2 b 5
select t2.id,t2.name,total from (select count(*) total from t1,t2 where t1.id = t2.id),t2;
select count(*) total from t1,t2 where t1.id = t2.id
total = 5
select count(*) total from t1,t2 where t1.id = t2.id where t2.id =1
total = 2
select count(*) total from t1,t2 where t1.id = t2.id where t2.id =2
total = 3select t2.id,t2.name,total from (select count(*) total from t1,t2 where t1.id = t2.id),t2;id name total
1 a 5
2 b 5我需要
id name total
1 a 2
2 b 3
where t2.id=t0.id;
from t2
只得到Total不为0时的记录;To bzszp(SongZip)
select t2.id,t2.name,sum(total) from (select count(*) total from t1,t2 where t1.id = t2.id),t2
group by t2.id,t2.name
结果仍为
id name total
1 a 5
2 b 5
where t2.id=t0.id(+)即可,谢谢;