Search code examples
mysqlsqlmysql-error-1054

#1054 - Unknown column 'a.group_id' in 'where clause'


Why does the query give me this error: #1054 - Unknown column 'a.group_id' in 'where clause' ?

SELECT a.group_id
FROM message_private a
INNER JOIN (SELECT group_id,profile_id
     FROM message_group b
     WHERE a.group_id = b.group_id AND b.profile_id = 's'
) as b ON b.group_id = a.group_id 

Solution

  • You are attempting to use the table alias a inside of your subquery which is causing the error. You should be able to write the query the following way:

    SELECT a.group_id 
    FROM message_private a 
    INNER JOIN message_group b
      ON b.group_id = a.group_id 
    WHERE b.profile_id = 's';
    

    If you need a subquery, then the syntax would be:

    SELECT a.group_id 
    FROM message_private a 
    INNER JOIN 
    (
      SELECT group_id,profile_id 
      FROM message_group
      WHERE profile_id = 's'
    ) b 
      ON b.group_id = a.group_id