SELECT
users.first_name,
users.email_address,
users.tp_user_id,
users.tp_username,
auth_token_log.module_access,
auth_token_log.created_date
FROM
users
INNER JOIN auth_token_log ON users.id = auth_token_log.user_id
WHERE
auth_token_log.id in(
SELECT
max(id)
FROM
auth_token_log
WHERE
auth_token_log.user_id = users.id
)
自己的理解
SELECT
users.first_name,
users.email_address,
users.tp_user_id,
users.tp_username,
auth_token_log.module_access,
auth_token_log.created_date
FROM
users
INNER JOIN auth_token_log ON users.id = auth_token_log.user_id
WHERE
auth_token_log.id in(
SELECT
max(auth_token_log.id)
FROM
auth_token_log,
users
WHERE
auth_token_log.user_id = users.id
GROUP BY
users.id
)
对于原始写法的理解是
先查出
SELECT
×
FROM
users
INNER JOIN auth_token_log ON users.id = auth_token_log.user_id
的记录, 然后针对每一行记录X,拿出这一行X与 一个新的auth_token_log表做join,然后筛选出 log.user_id = x..user.id的所有记录, 然后查出max(id), 这就是最新的log记录的 id