tags:

views:

280

answers:

1

The query used inside the IN() returns: 1, 2. However the whole query returns 0 rows which is impossible since they are present. What am I doing wrong here?

SELECT DISTINCT 
  li.auto_id
FROM 
  links AS li
  JOIN group_data AS gi ON 
    li.auto_id = gi.autoid AND li.user_id = gi.userid
WHERE 
  gi.groupid IN 
  (
    SELECT 
      CAST( GROUP_CONCAT( gf.groupid ) AS CHAR )
    FROM 
      group_followers AS gf
    WHERE 
      gf.userid_ext = '1'
    GROUP BY 
      gf.userid_ext
  )
  AND li.keyword LIKE '%shiba%'

Thank you so much,

Ice

+2  A: 

From the docs:

GROUP_CONCAT(expr):
This function returns a string result with the concatenated non-NULL values from a group.

You get a string containing '1,2'. That is nothing you can feed to the IN clause, because it is a single value, not two.

Just do it without the GROUP_CONCAT and it will work:

gi.groupid IN 
(
  SELECT 
    gf.groupid
  FROM 
    ...
)
Tomalak
Thank you! I feel so silly, spent 3 hours figuring out the CAST AS CHAR part and I don't even need the whole GROUP_CONCAT. Good lesson learned, won't ever happen to me again.