开发者

MySQL sum + inner join query

Question:

  • Table 1: id.1 | name.Joe | etc.Some | ...Other | ...Data
  • Table 2: id.X | number.+1 123 555 9999 | useridfromtable1.1 -> Linking telefone with Joe
  • Table 3: id. X | number.+1 123 555 9999 | calls.55

I need a query that join the 3 tables and I only have the id (useri开发者_开发问答d) from the table 1. So, I need from userid -> grab the telephone -> from the telefone grab calls on table3.

Answer:

SELECT t1.name,t1.id,t2.number,t3.calls
  FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number

New Question?

Any chance you know how can I print the sum of calls on the result? after all these join I get about 10 lines of the same user and their respective calls based on the phone, what is correct for what I asked, now I need return all the calls in 1 line with the values:

sum | user ID | user Name


Maybe this will be useful:

SELECT SUM(t3.calls) FROM table1 t1 INNER JOIN table2 t2 ON t2.useridfromtable=t1.id INNER JOIN table3 t3 ON t3.number = t2.number


Try this query:

SELECT sum(t3.calls), t1.id, t1.name
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
GROUP BY t1.id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜