Thanaporn
Thanaporn

Reputation: 189

How to select in table and results sorted by another table

Hi I was trying to create mysql statement that fit to my requirement. 2 tables as below

postTable

post_id | from_id
100     | 1
100     | 2
100     | 3
100     | 4
100     | 5

loveTable

post    | uid
1       | 1
100     | 3
100     | 4
100     | 5
5       | 6

I want to select from_id from postTable where post_id=100 order by //uid that have post =100 in loveTable firstly.

Expecting result

from_id
 3
 4
 5
 1
 2

Can you please advise me what is the right select statement?

Upvotes: 0

Views: 84

Answers (2)

Himanshu
Himanshu

Reputation: 32602

Try this:

SELECT from_id FROM postTable pt
LEFT JOIN loveTable lt
ON pt.from_id = lt.uid
WHERE pt.post_id  = 100
ORDER BY lt.post desc

See this SQLFiddle

Upvotes: 2

sel
sel

Reputation: 4957

select p.from_id 
from postTable p left join lovetable o on p.from_id=o.uid
and o.post=100 
where p.post_id=100
order by o.uid is not null desc,p.from_id

SQL FIDDLE HERE.

Upvotes: 7

Related Questions