mySQL: Joining three tables - how?

前端 未结 2 399
情歌与酒
情歌与酒 2021-01-24 00:07

I have the following query in my application. It works well, but I need it to also contain the number of products that are associated with each manufacturer.

The current

相关标签:
2条回答
  • 2021-01-24 00:35
    select * 
    from `manufacturers` m
    inner join `languages` l on m.`lang` = l.`id` 
    left outer join (
        select manufacturerid, count(*) as ProductCount
        from Products
        group by manufacturerid
    ) pc on m.id = pc.manufacturerid
    order by l.`id` asc, m.`id` asc 
    
    0 讨论(0)
  • 2021-01-24 00:37
    SELECT `manufacturers`.*, `languages`.*, COUNT(`products`.`id`) AS NumberOfProducts
    FROM (`manufacturers`)
    JOIN `languages` ON `manufacturers`.`lang` = `languages`.`id`
    LEFT OUTER JOIN `products` ON 
          `products`.`manufacturerid` =  `manufacturers`.`manufacturerid`
    GROUP BY <Column list for manufacturers AND languages here>
    ORDER BY `languages`.`id` asc, `manufacturers`.`id` asc
    
    0 讨论(0)
提交回复
热议问题