Reputation: 3297
I'm trying to find a MySQL query for the following question:
How many articles are being supplied by exactly one supllier
The query I got so far is:
SELECT
COUNT(artikel.art)
FROM
artikel
JOIN
inkart ON artikel.art = inkart.art
JOIN
leverancier ON inkart.lev = leverancier.lev
GROUP BY artikel.art
HAVING COUNT(leverancier.lev) = 1
But this is giving me the wrong (no) result. I figured I have to use a subquery somewhere, but I have no idea how.
Upvotes: 0
Views: 58
Reputation: 5972
select count(artikel.art)
from artikel
where 1= (select count(*) from inkart
join leverancier on inkart.lev=leverancier.lev
where inkart.art=artikel.art
)
Upvotes: 1
Reputation: 263733
You only need to count unique leverancier.lev
, this will give list of artikel.art
SELECT artikel.art
FROM artikel
JOIN inkart ON artikel.art = inkart.art
JOIN leverancier ON inkart.lev = leverancier.lev
GROUP BY artikel.art
HAVING COUNT(DISTINCT leverancier.lev) = 1
if you want the count of articles,
SELECT COUNT(*)
FROM
(
SELECT artikel.art
FROM artikel
JOIN inkart ON artikel.art = inkart.art
JOIN leverancier ON inkart.lev = leverancier.lev
GROUP BY artikel.art
HAVING COUNT(DISTINCT leverancier.lev) = 1
) a
Upvotes: 1