如果我有两个表,
第一个table1
:
id name
1 John
2 Andrew
3 Serj
4 Mike
5 Adam
and Second table2
:
user_id count date
2 0 01-09-2011...
5 9 05-09-2011...
1 5 05-09-2011...
3 7 04-09-2011...
如何从table1
中选择用户并按table2
中的count
、date
的值排序(DESC)
我想要的结果:
1 -- Adam ( count = 9 , date = 05.. )
2 -- John ( count = 5 , date = 05.. )
3 -- Serj ( count = 7 , date = 04.. )
4 ...
5 ...
...
如果不可能,或者很难得到我想要的结果(看看我的结果),那么就按计数和日期排序。
select * from table1 t1
join table2 t2 on t1.id = t2.user_id
order by date desc, count desc
使用JOIN。
SELECT name from table1
JOIN table2 ON table1.id = table2.user_id
ORDER BY table2.count, table2.date DESC
请也试一下
select * from table1 t1
join table2 t2 on t1.id = t2.user_id
order by date, count desc
Using INNER JOIN
SELECT t1.name, t2.count, t2.date
FROM table1 AS t1 INNER JOIN table2 AS t2
ON t1.id = t2.user_id
ORDER BY t2.count DESC, t2.date DESC;