有兩張表user表字段:id,username,namearticle表字段:id,art_title,art_content,posterId,controllerIdarticle表中的posterId是外鍵,指向user表中的idarticle表中的controllerId也是外鍵,也指向user表中的id比如現(xiàn)在article中有一條數(shù)據(jù):art_id | art_title | art_content | posterId | controllerId1 | title | content | 1 | 2user表中有兩條數(shù)據(jù)user_id | username | name1 | leo | 小紅2 | john | 小明我現(xiàn)在要查尋article中的這條數(shù)據(jù),但我希望通過(guò)posterId和controllerId將poster和controller的信息都查尋出來(lái),請(qǐng)問(wèn)sql語(yǔ)句該怎么寫啊?我現(xiàn)在的查尋語(yǔ)句是這樣的:SELECT article.*,user.*FROM articleLEFT JOIN userON article.id = user.idWHERE art_id = 1查尋出來(lái)的結(jié)果是:art_id=>1,art_title=>title,art_content=>content,posterId=>1,controllerId=>2,//我現(xiàn)在想在上面的sql語(yǔ)句中根據(jù)controllerId把controller的信息也查尋出來(lái),請(qǐng)問(wèn)該怎么寫sql語(yǔ)句user_id=>1, //這是根據(jù)posterId得到的poster的信息username=>leo, //這是根據(jù)posterId得到的poster的信息name=>小紅 //這是根據(jù)posterId得到的poster的信息
1 回答

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
可以再連接一下
SELECT a.*, b.username,b.name,c.username,c.nameFROM article aLEFT JOIN user bON a.posterId = b.idLEFT JOIN user c ON a.controllerId = c.idWHERE art_id = 1
添加回答
舉報(bào)
0/150
提交
取消