Search code examples
mysqlsqlmysql-error-1054

selecting from 2 tables. MySql


How do i do select p.Quota without writing the long full name? right now i am doing

SELECT c.id, 
       c.UserName, 
       p.Quota, 
       cs.StatusName 
  FROM CUSTOMERS AS c, 
       PRODUCTS AS p 
 LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId=cs.CustomerStatusId 
 LIMIT 1 ;

I get the error:

ERROR 1054 (42S22): Unknown column 'c.StatusId' in 'on clause'

However the column does exit and this code works:

SELECT c.id, 
       c.UserName, 
       cs.StatusName 
  FROM CUSTOMERS AS c
  JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId = cs.CustomerStatusId 
 LIMIT 1 ;

Solution

  • You're mixing ANSI and non ANSI JOIN syntax with:

    SELECT c.id, 
           c.UserName, 
           p.Quota, 
           cs.StatusName 
      FROM CUSTOMERS AS c, 
           PRODUCTS AS p 
      LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId=cs.CustomerStatusId 
      LIMIT 1 ;
    

    Written using ANSI joins:

         SELECT c.id, 
                c.UserName, 
                p.Quota, 
                cs.StatusName 
           FROM CUSTOMERS AS c 
           JOIN PRODUCTS AS p ON --JOIN criteria goes here
      LEFT JOIN CUSTOMERSTATUSTYPES as cs ON c.StatusId = cs.CustomerStatusId 
          LIMIT 1;
    

    ...but I don't know what criteria you are using to join PRODUCTS to the CUSTOMERS table.