MySQL从第三个表中添加数据

时间:2016-02-17 21:31:10

标签: mysql join inner-join

SELECT m.member_name, p.agefield
FROM members m
JOIN profilefields p ON m.member_id = p.member_id
WHERE m.member_gid = 1

我想在第三个表'group'的查询中添加'prefix'和'suffix'值。 member_id是每个表中的关键,但我无法理解如何进行连接。应该怎么做?下面是非工作代码,只是为了看看我是否在正确的轨道上。谢谢。

SELECT m.member_name, p.agefield, g.prefix, g.suffix
FROM members m
INNER JOIN profilefields p ON m.member_id = p.member_id
INNER JOIN groups g ON g.member_id = p.member_id
WHERE m.member_gid = 1

1 个答案:

答案 0 :(得分:1)

将您的第三个加入更改为下面直接加入members表格。

INNER JOIN groups g ON g.member_id = m.member_id

您的查询应该是

SELECT m.member_name, p.agefield, g.prefix, g.suffix
FROM members m
INNER JOIN profilefields p ON m.member_id = p.member_id
INNER JOIN groups g ON g.member_id = m.member_id
WHERE m.member_gid = 1