There are two tables, Table 1 ->T1:
id name time type
1 A hh 1
2 B kk 1
3 c vv 2
Table 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
The pid of table 2 is associated with the id of table 1, when type=1 , hoping to get the following two pieces of data:
1 A hh 20 10 5 (35)
2 B kk 15 10 (25)
Please tell me how to write the sql statement, thank you! PS: The thinkPHP framework is used
世界只因有你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;