mysql join问题

新手上路,请多包涵

有两张表
user表字段:
id,username,name

article表字段:
id,art_title,art_content,posterId,controllerId

article表中的posterId是外键,指向user表中的id
article表中的controllerId也是外键,也指向user表中的id

比如现在article中有一条数据:

art_id | art_title | art_content | posterId | controllerId
1      | title     | content     | 1        | 2

user表中有两条数据

user_id | username | name
1       | leo      | 小红
2       | john     | 小明

我现在要查寻article中的这条数据,但我希望通过posterId和controllerId将poster和controller的信息都查寻出来,请问sql语句该怎么写啊?

我现在的查寻语句是这样的:

SELECT article.*,user.*
FROM article
LEFT JOIN user
ON article.id = user.id
WHERE art_id = 1

查寻出来的结果是:

art_id=>1,
art_title=>title,
art_content=>content,
posterId=>1,
controllerId=>2,//我现在想在上面的sql语句中根据controllerId把controller的信息也查寻出来,请问该怎么写sql语句
user_id=>1,    //这是根据posterId得到的poster的信息
username=>leo, //这是根据posterId得到的poster的信息
name=>小红     //这是根据posterId得到的poster的信息
阅读 1k
1 个回答

可以再连接一下

SELECT a.*, b.username,b.name,c.username,c.name
FROM article a
LEFT JOIN user b
ON a.posterId = b.id
LEFT JOIN user c ON a.controllerId = c.id
WHERE art_id = 1
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题