我想加入3个表
Table employer: +-------------+-------------+---------+ | employer_id | position_id | shop_id | +-------------+-------------+---------+ | 1 | 2 | 2 | | 2 | 2 | 2 | | 3 | 2 | 2 | | 4 | 1 | 1 | | 5 | 1 | 2 | +-------------+-------------+---------+ Table position: +-------------+-----------------+ | position_id | position_name | +-------------+-----------------+ | 1 | Receptionist | | 2 | Secretary | +-------------+-----------------+ Table shop: +---------+-----------+ | shop_id | shop_name | +---------+-----------+ | 1 | Shop ABC | | 2 | Shop DEF | +---------+-----------+
这是我的问题:
SELECT S.*, P.*, E.*, COUNT(E.employer_id) AS NumberOfJobs FROM ((shop AS S INNER JOIN employer AS E ON S.shop_id=E.shop_id) INNER JOIN position AS P ON P.position_id=E.position_id) GROUP BY S.shop_id;
我得到的结果:
Shop ABC Receptionist Job Available : 1 Shop DEF Secretary Job Available : 4
但我想要的是:
Shop ABC Receptionist Job Available : 1 Shop DEF Receptionist Job Available : 1 Secretary Job Available : 3
获得我想要的结果的正确查询是什么?感谢有人可以帮我解决这个问题。非常感谢你!
答案 0 :(得分:1)
您需要按店铺和位置进行分组,而不仅仅是购物:
SELECT S._Id , S.Shop_Name
P.Position_Name ,
COUNT(E.employer_id) AS NumberOfJobs
FROM shop AS S
INNER JOIN employer AS E ON S.shop_id = E.shop_id
INNER JOIN position AS P ON P.position_id = E.position_id
GROUP BY S.shop_id, S.Shop_Name, P.Position_Name