简体   繁体   中英

MySQL query works in phpMyAdmin but not at site. Unknown column 'zone' in 'where clause'

The following query works in phpMyAdmin but doesn't work when I run it in through the website in PHP.

SELECT * FROM 
      (SELECT name, zone FROM staff 
        LEFT OUTER JOIN zones 
          ON staff.suburb=zones.suburb
      ) A WHERE zone='2'

The following query also doesn't work on the website but works in phpMyAdmin:

SELECT name, zone FROM staff 
  LEFT OUTER JOIN zones 
    ON staff.suburb=zones.suburb 
  WHERE zone='2'

Both give an error:

Unknown column 'zone' in 'where clause'.

What am I doing wrong here?

If you want to run the first query the way you provided, it should be more like this:

SELECT * 
FROM (
    SELECT name,zone 
    FROM staff 
    LEFT OUTER JOIN zones ON staff.suburb=zones.suburb
) A 
WHERE A.zone='2'

But I totaly do not understand why you are doing a SUB-SELECT, that is totally unnecessary. The second query should work, unless you have made a typographical error. Also it is good practice to escape every column, or table name used in query, like so:

SELECT `name`, `zone` 
FROM `staff` s 
LEFT OUTER JOIN `zones` z 
    ON s.`suburb` = z.`suburb` 
WHERE z.`zone` = '2'

Try this :

SELECT `name`, `zone`
FROM `staff` AS s
LEFT OUTER JOIN `zones` AS z ON `s`.`suburb`=`z`.`suburb`
WHERE `s`.`zone`='2';

Supposing that the column zone is in the table staff . If it's in the table zones , change the WHERE to :

WHERE `z`.`zone`='2';

The key part here is the precision in the WHERE clause. Maybe MySQL don't know where to look up (in which table), so adding a precision on the table will reduce the risk. Moreover, make sur this column exists in that table (and with that typo. zones or Zone could lead to errors).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM