MySQL Query (ORDER BY other table)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

MySQL Query (ORDER BY other table)

Post by Hermit TL »

I want to ORDER a query based on a value in a different table.
For example, say the query is

Code: Select all

SELECT links FROM table1
I have another table with a column that holds only a 1 or a 0. So how would I order it by that columns? (the linkname column is the same in both tables)
Anyone know how to do this?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: MySQL Query (ORDER BY other table)

Post by twinedev »

You will need to do a join something like:

Code: Select all

SELECT t1.`links` 
FROM `table1` AS t1 
JOIN `table2` AS t2 
  ON t1.`linkname`=t2.`linkname` 
ORDER BY t2.`othercolumn`
Post Reply