我正在寻找一种方法来从用户表中选择在另一个表中至少有一个相关条目的用户。可能我的加入方法是完全错误的,但这正是我的尝试,应该让你了解结构:
SELECT users.`id`, users.`email`, users.`username`, users.`status`, users.`date_reg`
FROM dir_editors as users
JOIN dir_links as links ON (links.`id_editor` = users.`id`)
WHERE COUNT(links.`id_editor`) > 1
因此,目标是从dir_links
表中至少有一个链接条目的用户获取所有这些用户数据,其中id_editor
是dir_links表中的字段。
感谢您的帮助
答案 0 :(得分:4)
SELECT users.`id`, users.`email`, users.`username`, users.`status`, users.`date_reg`
FROM dir_editors as users
WHERE EXISTS(SELECT 1 FROM dir_links as links WHERE links.`id_editor` = users.`id`)
答案 1 :(得分:0)
SELECT users.`id`, users.`email`, users.`username`, users.`status`, users.`date_reg`
FROM dir_editors as users
JOIN dir_links as links ON (links.`id_editor` = users.`id`)
删除此WHERE COUNT(links.
id_editor ) > 1
,因为这种情况很明显。
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
答案 2 :(得分:0)
INNER JOIN选择在两个表中找到的记录,具体取决于您在ON语句中放置的键:
SELECT users.id, users.email, users.username, users.status, users.date_reg
FROM dir_editors AS users
INNER JOIN dir_links as links
ON links.id_editor = users.id