Jump to content

JOIN AND ORDER BY


Go to solution Solved by DaveyK,

Recommended Posts

Currently I have a query set to do a search based upon no TimeOut and for a particular location using the $tablename table. That looks like:

 

$query = "SELECT * FROM $tablename WHERE TimeOut = '0000-00-00 00:00:00' AND Location = '$location'";

 

This ^ works just fine.

 

Now, one of the columns in $tablename is Position. In a different table ($tabletwo) I have a table that lists the same positions and an associated rank (a Manager would be 1, a Supervisor 2, Worker 3, etc.).

 

So in both tables, the common denominator is the Position/Positions column.

 

I would like to order the results based upon the column Position(s) using the rank established in $tabletwo (so individuals identified as Manager would be shown near the top instead of scattered throughout).

 

I can't seem to figure this out. I tried:

 

SELECT * FROM $tablename WHERE TimeOut = '0000-00-00 00:00:00' AND Location = '$location' as t1 LEFT JOIN $tabletwo AS t2 on t1.Position = t2.Positions ORDER BY t2.Rank

 

amongst about a hundred other things and can't seem to get it to work.

 

Any help would be appreciated.

 
 

Link to comment
https://forums.phpfreaks.com/topic/275808-join-and-order-by/
Share on other sites

  • Solution

where table two only holds the info as to what rank what position has...

 

 

SELECT * 
FROM $tablename  as t1
LEFT JOIN $tabletwo AS t2 
on t1.Position = t2.Positions
WHERE t1.TimeOut = '0000-00-00 00:00:00' 
AND t1.Location = '$location'
ORDER BY t2.Rank DESC
 

and this doesnt work?

Edited by DaveyK
Link to comment
https://forums.phpfreaks.com/topic/275808-join-and-order-by/#findComment-1419302
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.