简体   繁体   中英

Mysql Unknown column in where clause

SELECT t1.`ID`, t1.`notification_type`, t1.`notification_by`, t1.`notification_by_username`, 
                t1.`notification_status`, t1.`notification_date`, t1.`school_key`, t1.`class_key`, t1.`post_key`, t1.`extra`, t1.`class_info`, t1.`class_subject`,
                (SELECT `notification_last_check` 
                       FROM data_users.account_info t2
                       WHERE t2.`user_key` = t1.`notification_by` LIMIT 1) AS `notification_last_check` 
       FROM `14754931095281411` t1 
       WHERE t1.`notification_status` = '10' 
                AND t1.`notification_date` > t2.`notification_last_check`   
       ORDER BY `notification_date` DESC LIMIT 10;

1054 - Unknown column 't2.notification_last_check' in 'where clause'

why im getting the above error ?

t2 exist only in the sub-query scope (the query in your select).

So here you don't have t2.notification_last_check but only notification_last_check

SELECT ...,
            (SELECT `notification_last_check` 
                   FROM data_users.account_info t2
                   WHERE t2.`user_key` = t1.`notification_by` LIMIT 1) AS `notification_last_check` 
   FROM `14754931095281411` t1 
   WHERE t1.`notification_status` = '10' 
            AND t1.`notification_date` > `notification_last_check`  -- Remove the alias t2
   ORDER BY `notification_date` DESC LIMIT 10;

Then of course, I should mention that this will failed because you can't use an alias in WHERE clause. Only in GROUPB BY , ORDER BY and HAVING . You could use HAVING to set this condition, read about this one if you don't know it already.

And other solution would be to use a inner join (a small change is needed)

SELECT ...,
       t3.`notification_last_check` 
   FROM `14754931095281411` t1 
   LEFT JOIN (
         SELECT DISTINCT t2.`user_key`, `notification_last_check` 
                  FROM data_users.account_info t2 
         ) t3 On t3.`user_key` = t1.`notification_by
   WHERE t1.`notification_status` = '10' 
   AND t1.`notification_date` > t3.`notification_last_check` 
   ORDER BY t1.`notification_date` DESC LIMIT 10;

A possible solution:

SELECT
    t1.`ID`, t1.`notification_type`, t1.`notification_by`,
    t1.`notification_by_username`, 
    t1.`notification_status`, t1.`notification_date`, t1.`school_key`,
    t1.`class_key`, t1.`post_key`, t1.`extra`, t1.`class_info`,
    t1.`class_subject`,
    (SELECT `notification_last_check` 
    FROM data_users.account_info t2
    WHERE t2.`user_key` = t1.`notification_by` LIMIT 1) AS `notification_last_check` 
FROM `14754931095281411` t1 
WHERE t1.`notification_status` = '10' 
AND t1.`notification_date` > (SELECT `notification_last_check` 
    FROM data_users.account_info t3
    WHERE t3.`user_key` = t1.`notification_by` LIMIT 1) 
ORDER BY `notification_date` DESC LIMIT 10;

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