-1

Good day. I'm trying and failing to write a mySql query that will return custid only if that custid has multiple values. In the example data below I want to return custid only for customers with type 2,5 & 7 set so only custid 1440 in the result. Can anyone help?

custid type
1436 7
1436 5
1437 2
1438 2
1439 5
1440 2
1440 5
1440 7
1441 2
1441 5
1442 7
1443 5
1444 7

Many Thanks.

1
1

You need HAVING COUNT condition =3 and type in (2,5,7) to produce your expected result.

Try:

SELECT custid
FROM test_tbl
WHERE type in (2,5,7)
GROUP BY custid
HAVING COUNT(custid)=3;

DEMO: https://www.db-fiddle.com/f/qff694udysNgqbyJyFcDzn/7

1
  • Many Thanks Ergest
    – John Mac
    Nov 5 '21 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.