Why doesn’t this work?
SELECT `user_log`.`COMPUTER_NAME`, `user_log`.`LOCATION`
FROM `user_log`, `user_log_max` as max
WHERE max.`USER_NAME` = `user_log`.`USER_NAME` AND max.`ACCESS_DATE_TIME` = `user_log`.`ACCESS_DATE_TIME`
I get the following error:
#1054 - Unknown column 'max.ACCESS_DATE_TIME' in 'where clause'
I have tried taking out the alias:
SELECT `user_log`.`COMPUTER_NAME`, `user_log`.`LOCATION`
FROM `user_log`, `user_log_max`
WHERE `user_log_max`.`USER_NAME` = `user_log`.`USER_NAME` AND `user_log_max`.`ACCESS_DATE_TIME` = `user_log`.`ACCESS_DATE_TIME`
and this error ->
#1054 - Unknown column 'user_log_max.ACCESS_DATE_TIME' in 'where clause'
It’s taking out the tick marks…why?
EDIT:
user_log_max:
USER_NAME varchar(20)
ACCESS_DATE_TIME timestamp
user_log contains the same column, plus a few more including COMPUTER_NAME and LOCATION
user_log_max contains all the data from issuing this query on user_log
SELECT `USER_NAME`, MAX(`ACCESS_DATE_TIME`) FROM `user_log
GROUP BY `USER_NAME`
Essentially what I’m trying to do is select columns withing a MAX…GROUP BY other than what is in my GROUP BY.
EDIT 2:
Here is how I’m creating user_log_max ->
CREATE TABLE `user_log_max`
SELECT `USER_NAME`, MAX(`ACCESS_DATE_TIME`) FROM `user_log`
GROUP BY `USER_NAME`
In your
CREATE TABLEquery, you must supply an alias for theMAX()aggregate column:The way you had done it, the column name should be something like the whole string
MAX(ACCESS_DATE_TIME), which is obviously very confusing.Now, you can query it as you attempted to do: