Jump to content

[SOLVED] ORDER BY based on Results FROM Another table


JonneDs

Recommended Posts

So here i come,my first post and i'm already asking for help, huh? so lets go straight on the point....

 

I have a query that , and i need to order the result based on wheter or not the ID of my main table appears on another table, somewhat like this:

 

SELECT A.id AS Id, A.nome AS Nome

    FROM main_table A

    JOIN joined_table JT ON JT.id = A.id_cat

ORDER BY ("first whoever have a row in" another_table AT)

 

another_table  stores the image paths related to  main_table  , i need to show all query results from main_table  even if they don't have a image (row in another_table), but i need to show whoever have a image (a row in another_table) first...

 

I've tryied to join with another_table and even to use sobqueryes in ORDER B... but so far i got no results =/

it's even worst because this query is a monter with multiple lines (the sample is simplyfied...)

 

Any help would be apreciated!!

Link to comment
Share on other sites

Like

SELECT 
A.id AS Id, 
A.nome AS Nome,
JT.id as OB_FIELD
FROM main_table A
    JOIN joined_table JT ON JT.id = A.id_cat

ORDER BY OB_FIELD

 

something like this  but not for JT , for Another table  (AT)...

 

the problem is this "AT" table have multiple references to Main_table and i cant use a ON clause because it would return only the rows in Main_Table that have a row in Another_Table, and thats not what i need...

Link to comment
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.