Heim > Fragen und Antworten > Hauptteil
现有两个表格,表格1 ->T1:
id name time type
1 A hh 1
2 B kk 1
3 c vv 2
表格2-> T2:
id pid every
1 1 20
2 1 10
3 1 5
4 2 15
5 2 10
6 3 25
7 3 35
其中表格2的pid关联表格1的id,当type=1时,希望得到如下两条数据:
1 A hh 20+10+5(35)
2 B kk 15+10 (25)
求教sql语句该怎么写啊,谢谢!PS:用的是thinkPHP框架
世界只因有你2017-05-16 13:02:01
select t1.id, t1.name, t1.time, sum(t2.every)
from t1
inner join t2 on t1.id=t2.pid
group by t1.id, t1.name, t1.time
PHP中文网2017-05-16 13:02:01
SELECT T1.id,T1.name,T1.time,SUM(T2.every)
FROM T1
INNER JOIN T2 ON T1.id = T2.pid
GROUP BY T1.id;