Question

I got 3 tables in my MYSQL bases and I have to compare how many time there are each user_ID in each of the 2 first table (table 1 and table 2)

here is my table 1:

user_ID

  • A
  • B
  • A
  • D
  • ...

here is my table 2 :

user_ID

  • A
  • C
  • A
  • ...

here is my table 3 (with link between user_ID and nickname) :

user_ID // nickname

  • A // Bob
  • B // Joe
  • C // Tom
  • ...

I would like to get a result like this:

Nickname // count occurrences from Table 1 // count occurrences from table 2

  • Bob // 1 // 2
  • Joe // 4 // 0
  • Tom // 0 // 2

I did not succeed for instant to count separately from each table, I got a global result for each nickname :( Could you help me to find the right MYSQL request ? - ...

No correct solution

OTHER TIPS

This type of query is a little tricky, because some names may not be in the first table and others may not be in the second. To really solve this type of problem, you need to pre-aggregate the results for each query. To get all the names, you need a left outer join:

select t3.name, coalesce(cnt1, 0) as cnt1, coalesce(cnt2, 0) as cnt2
from table3 t3 left outer join
     (select name, count(*) as cnt1
      from table1
      group by name
     ) t1n
     on t3.name = t1n.name left outer join
     (select name, count(*) as cnt2
      from table2
      group by name
     ) t2n
     on t3.name = t1n.name;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top