Php 按另一个表中的数据排序行
如果我有两张桌子 第一个Php 按另一个表中的数据排序行,php,mysql,sql-order-by,Php,Mysql,Sql Order By,如果我有两张桌子 第一个表1: id name 1 John 2 Andrew 3 Serj 4 Mike 5 Adam 第二个表2: user_id count date 2 0 01-09-2011... 5 9 05-09-2011... 1 5 05-09-2011... 3 7 04-09-2011... 我如何从表1中选择用户,并按计
表1
:
id name
1 John
2 Andrew
3 Serj
4 Mike
5 Adam
第二个表2
:
user_id count date
2 0 01-09-2011...
5 9 05-09-2011...
1 5 05-09-2011...
3 7 04-09-2011...
我如何从表1
中选择用户,并按计数
,日期
值从表2
中排序(DESC)
我想要的结果是:
1 -- Adam ( count = 9 , date = 05.. )
2 -- John ( count = 5 , date = 05.. )
3 -- Serj ( count = 7 , date = 04.. )
4 ...
5 ...
...
如果不可能,或者很难得到我想要的结果(请参见我的结果),则只需按计数和日期排序。使用JOIN
select * from table1 t1
join table2 t2 on t1.id = t2.user_id
order by date desc, count desc
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
使用
连接两个表并对结果进行排序。谢谢。。。!但是仔细看我的结果(约翰的数量比塞尔吉的少,但因为日期的关系,约翰在之前),我能有这样的东西吗?
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;