简体   繁体   中英

MySQL query: merge rows in three tables and output as one

I want to extract information from three tables which are linked by certain ids to each other. Can you please suggest me how can i do that. I tried but three tables is bit too complex. Here is what i am trying to do:

这是三个表和结果表

So basically from above three tables i want to match USER against respective PID showing the count of only specific SPEC type, eg ROUND as shown in the example. (It can also happen that same PID is assinged to two different users like PID=1 for user AAA & BBB).

Can you please tell me how to go about it?

SELECT 
  t1.USER, 
  t1.PID, 
  COUNT(T3.SPEC) AS CountRound
FROM Table1 t1
  LEFT JOIN Table2 t2 ON t1.PID = t2.PID 
  LEFT JOIN Table3 t3 ON t2.LID = t3.LID AND t3.SPEC = 'ROUND'
GROUP BY
  t1.USER,
  t1.PID

Something to start with?

SELECT 
 T1.USER, 
 T1.PID, 
 IFNULL(COUNT(T3.SPEC), 0) 
FROM 
 Table1 T1 LEFT JOIN Table2 T2 
  ON T1.PID = T2.PID 
 JOIN Table3 T3 
  ON T2.LID = T3.LID 
WHERE T3.SPEC = 'ROUND'
    select user,pid,count(*) from (

    select table1.user,table1.pid,table3.spec 
    from 
      table1,
      table2,
      table3 
    where 
      table1.pid=table2.pid(+) 
    and table2.lid=table3.lie(+) 
    and table3.spec='Round' 

) 
group by user,pid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM