0

I have the following MySQL query:

SELECT l.user_id, u.ID, u.ULN, count(u.ULN) As ULN_count
FROM table_Learning l
LEFT JOIN table_users u 
ON l.ULN = u.ULN
WHERE l.user_id is null
AND (SELECT COUNT(ULN) 
    FROM Table_users 
    WHERE ULN = u.ULN 
    GROUP BY ULN) < 2
GROUP BY u.ULN

What I would expect from this query is that The value of the field ULN_Count cannot be greater than 1.

However, the query does not achieve this as the filed has values greater than 1.

What am I doing wrong?

6
  • 1
    Please show some sample data from the two involved tables. It is possible you are running afoul of MySQL's lenient GROUP BY behavior because you have several columns in SELECT not also included in GROUP BY and not aggregated. That will often cause indeterminate groupings, and results in a compile error in most RDBMS besides MySQL. Commented Jun 23, 2015 at 13:21
  • 1
    But looking at the structure of your subselect in WHERE, it seems like that entire thing may be eliminated from WHERE and replaced with HAVING COUNT(u.ULN) < 2 Commented Jun 23, 2015 at 13:23
  • @MichaelBerkowski if you put that as an answer I will mark it as correct Commented Jun 23, 2015 at 13:25
  • @MichaelBerkowski yes Commented Jun 23, 2015 at 13:26
  • @MichaelBerkowski yes but you answered it so i am not marking the one below as correct Commented Jun 23, 2015 at 13:27

2 Answers 2

1

Your subselect in the WHERE clause is making comparisons on the aggregate COUNT(u.ULN) value, and therefore really belongs in a HAVING clause. It should be significantly more performant there, because the subselect would be evaluated for every row returned by the outer query while the HAVING filter will only be applied once for the entire aggregate query.

SELECT
  l.user_id,
  u.ID, u.ULN,
  count(u.ULN) As ULN_count
FROM
  table_Learning l
  LEFT JOIN table_users u 
    ON l.ULN = u.ULN
WHERE l.user_id is null
GROUP BY u.ULN
-- The aggregate can be compared & filtered in HAVING
-- which is applied just once for the whole query
-- and you can use the alias for comparison
HAVING ULN_count < 2

Without seeing your sample data, it is possible that MySQL's default GROUP BY behavior may cause you some grief. All columns in the SELECT which are not part of an aggregate like COUNT(),SUM(),MAX() should also be included in the GROUP BY, in particular if you are joining multiple tables. In something other than a one-to-one relationship, MySQL may return indeterminate results for the ungrouped columns (l.user_id in this context).

Sign up to request clarification or add additional context in comments.

Comments

1

You have to move this subquery to having state like this

SELECT 
     l.user_id, u.ID, u.ULN, count(u.ULN) As ULN_count
FROM 
    Table_Learning l
    LEFT JOIN table_users u ON l.ULN = u.ULN
WHERE 
     l.user_id is null
GROUP  BY u.ULN
HAVING  count(u.ULN) < 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.