加入四张桌子

时间:2011-07-26 20:34:18

标签: mysql sql

我有三个表,我想知道如果我应该加入,表的结构如下:

members                                                          

member_id    login    


permissions

K_id    member_id   date


kcontent

K_id    id    content  


ktitle

K_id          title

我想要做的是,对于特定成员(member_id)....获取每个成员链接到的每个K_id,按日期DESC排序,然后从id按顺序获取内容和来自ktitle的标题

2 个答案:

答案 0 :(得分:0)

是的,你应该。

尝试以下几点:

SELECT a.member_id, b.date, d.title, c.content
FROM members as a
JOIN permissions as b
ON b.member_id = a.member_id
JOIN kcontent as c
ON c.k_id = b.k_id
JOIN ktitle as d
ON d.k_id = b.k_id
WHERE a.member_id = {inputMemberId}
ORDER BY b.date, d.k_id, d.title

答案 1 :(得分:0)

SELECT *
FROM 
PERMISSIONS INNER JOIN KCONTENT
ON PERMISSIONS.K_ID = KCONTENT.K_ID
INNER JOIN KTITLE
ON KCONTENT.K_ID = KTITLE.K_ID
INNER JOIN MEMEBRS
ON MEMBERS.MEMBER_ID = PERMISSIONS.MEMBER_ID
WHERE MEMBERS.MEMBER_ID = ###
ORDER BY PERMISSIONS.DATE, MEMBERS.MEMBER_ID