[英]Adding a new column from table to mysql query
我用了一段时间Stack,你帮了我很多……这是我的第一个问题,希望能够正确暴露。
我需要从一个表中检索所有值,并从另一个表中添加一个带有总和值的列。 这就是我的意思:
q1=>
SELECT * FROM users WHERE role >30 AND role <50 AND available = 1 AND active = 1
到 q1 从 q2=> 添加一个新列
SELECT SUM(budget) AS totalBudget FROM projects_assignments
例子:
table1 = idUser
,一些值
table2 = idUser
, budget
,其他 es:值 (1,10000,40),(1,5000,30),(2,5000,30)
我的预期结果是:
idUser,table1column1,table1column2,totalBudget
1,x,y,15000(10k+5k)
2,x,x,5000
3,x,y,0
...
n,x,y,0
非常感谢,马特奥
select table1.column1,
table1.column2,
table1.column3,
sum(table2.budget) as totalBudget
from table1
inner join table2
on table1.idUser = table2.idUser
where...
group by table1.idUser
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.