Mysql 选择其他表中没有外键的主键

Mysql 选择其他表中没有外键的主键,mysql,sql,database,one-to-many,Mysql,Sql,Database,One To Many,为了简化,我有两个使用外键与一对多相关的表,例如: Users table: id name Actions table: id user_id 一个用户可能有许多操作,也可能没有。我需要一个sql select,它返回操作表中没有用户id值的用户id Users Table: id name 1 John 2 Smith 3 Alice Actions Table: id user_id 1 3 2 1 因

为了简化,我有两个使用外键与一对多相关的表,例如:

Users table:
id
name

Actions table:
id
user_id
一个用户可能有许多操作,也可能没有。我需要一个sql select,它返回操作表中没有用户id值的用户id

Users Table:
id      name
1       John
2       Smith
3       Alice

Actions Table:
id      user_id
1       3
2       1
因此,我需要一个返回用户id 2(Smith)的sql查询,因为外键值不包括id 2

我尝试了以下SQL,但它返回所有用户ID:

SELECT users.id from users left join actions on actions.user_id is null

优化版本为:

SELECT u.id
FROM users u
LEFT JOIN actions a
ON a.user_id = u.id
AND ISNULL(a.user_id)

你是对的,我没有输入正确的是:在a.user\u id=u.idYeah上,我的查询中有一个输入错误。
SELECT u.id
FROM users u
LEFT JOIN actions a
   ON a.user_id = u.id
WHERE a.user_id IS NULL
SELECT u.id
FROM users u
LEFT JOIN actions a
ON a.user_id = u.id
AND ISNULL(a.user_id)