我有第一个用户表,它有一个 bool 字段来将用户表示为 active
,在第二个表中我有用户 audits
。
我必须找到用户上次激活的时间,这意味着我有很多 audits
只需要为当前 上次激活
用户。audit
>活跃
Fields of User Table:
user_id, username, firstname, lastname, email, isactive
Fields of Audit Table:
user_id, audit_date, actiontype
最佳答案
我猜你想要这个:
SELECT
user_table.*, MAX(audit_table.audit_date) AS last_activated
FROM audit_table
LEFT JOIN user_table ON (user_table.user_id = audit_table.user_id)
WHERE
audit_table.user_id = 5 AND audit_table.actiontype = 'activate'
AND user_table.isactive = 1;
如果您想要包含上次激活日期的激活用户列表:
SELECT
user_table.*, MAX(audit_table.audit_date) AS last_activated
FROM audit_table
LEFT JOIN user_table ON (user_table.user_id = audit_table.user_id)
WHERE
audit_table.actiontype = 'activate' AND user_table.isactive = 1
GROUP BY audit_table.user_id;
关于mysql - 如何在mysql中的第一个表中找到记录的最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38051342/