我有2个表,Pages和LinkPages 在Pages中,我有以下字段:
在LinkPages中,我有:
我将使用什么SQL查询来获取具有特定ID的页面上的所有信息,然后在页面启动日期之前对其进行排序。
我到目前为止:
SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID' LIMIT 5
但显然我没有在这里订购它们,我需要使用连接吗?
非常感谢,
洁
答案 0 :(得分:5)
试试这个:
SELECT * FROM LinkPages
INNER JOIN Pages ON Pages.pageID = LinkPages.pageID
WHERE linkmemberID='MEMBERID'
ORDER BY startDate DESC
LIMIT 5
答案 1 :(得分:2)
SELECT lp.pageID, lp.linkmemberID, lp.joinDate
FROM LinkPages lp, Pages p
WHERE lp.linkmemberID='MEMBERID' AND lp.pageID = p.pageID
ORDER BY p.startDate DESC
LIMIT 5
答案 2 :(得分:1)
您有两个选项,可以JOIN
,也可以使用子查询:
SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID'
ORDER BY
(SELECT startDate FROM Pages WHERE Pages.pageID = LinkPages.PageID) DESC
LIMIT 5
为了更好的衡量,这是加入:
-- be sure to use L.* here, otherwise you get all of the columns from
-- pages as well
SELECT L.* FROM LinkPages L
INNER JOIN Pages P ON P.pageID = L.pageID
WHERE linkmemberID='MEMBERID'
ORDER BY P.startDate DESC LIMIT 5
答案 3 :(得分:0)
SELECT lp.*
FROM LinkPages lp, Pages p
WHERE lp.pageId = p.pageId
AND lp.linkmemberID='MEMBERID'
ORDER BY p.startDate
LIMIT 5
抱歉 - 忘记了ORDER BY ...