Jai
Jai

Reputation: 2206

Ordering a mysql query?

I have 2 tables, Pages and LinkPages.
Within Pages i have the fields:

In LinkPages I have:

What sql query would i use to get all information on the pages with a particular id and then order it by the date the page was started.

I got this far:

SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID' LIMIT 5

but obviously i haven't ordered them here, would i need to use a join?

Many Thanks,

Jai

Upvotes: 1

Views: 88

Answers (4)

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102398

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

Upvotes: 2

cwallenpoole
cwallenpoole

Reputation: 81988

You have two options, either you can JOIN or you can use a subquery:

SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID' 
ORDER BY 
   (SELECT startDate FROM Pages WHERE Pages.pageID = LinkPages.PageID) DESC
LIMIT 5

For good measure, here's the join:

-- 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

Upvotes: 1

HBublitz
HBublitz

Reputation: 680

SELECT lp.* 
FROM LinkPages lp, Pages p 
WHERE lp.pageId = p.pageId
AND lp.linkmemberID='MEMBERID' 
ORDER BY p.startDate
LIMIT 5

sorry - forgot the ORDER BY ...

Upvotes: 0

Paul S.
Paul S.

Reputation: 1537

Try this:

SELECT * FROM LinkPages
INNER JOIN Pages ON Pages.pageID = LinkPages.pageID
WHERE linkmemberID='MEMBERID'
ORDER BY startDate DESC
LIMIT 5

Upvotes: 5

Related Questions