Quantcast
Channel: mysql "and" logic within result set - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by fthiella for mysql "and" logic within result set

$
0
0

This should return the rows that you want:

SELECT foo.*
FROM
  foo
WHERE
  employeeID IN (
    SELECT employeeID
    FROM foo
    GROUP BY employeeID
    HAVING COUNT(DISTINCT employeeType) =
      (SELECT COUNT(DISTINCT employeeType)
       FROM foo)
  )

Please see a fiddle here.

The inner query will return the number of distinct employee types:

(SELECT COUNT(DISTINCT employeeType) FROM foo)

The middle query will return all the employee IDs that have the maximum number of employee types:

SELECT employeeID
FROM foo
GROUP BY employeeID
HAVING COUNT(DISTINCT employeeType) =
       (SELECT COUNT(DISTINCT employeeType) FROM foo)

and the outer query will return the whole rows.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>