我一起用php,mysql和html来显示表格。 但是我希望按时间戳对项目进行排序,并将具有相同ID的项目组合在一起。 例如:
Unsorted table:
timestamp id name description
14:02 2 Hans Is very nice!
12:01 3 Jürgen Is very bad!
10:03 2 Hans Again good.
11:08 6 Anna BAD!
06:09 11 Peter Good.
Right sorted table:
timestamp id name description
06:09 11 Peter Good.
10:03 2 Hans Again good.
14:02 2 Hans Is very nice!
11:08 6 Anna BAD!
12:01 3 Jürgen Is very bad!
Bad sorted table:
timestamp id name description
06:09 11 Peter Good.
10:03 2 Hans Again good.
11:08 6 Anna BAD!
12:01 3 Jürgen Is very bad!
14:02 2 Hans Is very nice!
我有以下但不工作:
SELECT * FROM fehlerfilm ORDER BY timestamp, imdb_id
我得到Bad sorted table。
我想要最后的所有物品! 过程:
答案 0 :(得分:1)
派生表具有相关的子查询,该子查询返回每个imdb_id的最低时间戳。按该栏目排序:
SELECT timestamp, id, name, description
from
(
select f1.*, (select min(timestamp) from fehlerfilm f2
where f2.imdb_id = f1.imdb_id) as mintime
from fehlerfilm f1
)
ORDER BY mintime, imdb_id, timestamp
答案 1 :(得分:-2)
如果您想先获取最新数据,可以使用以下查询,该查询将按ID分组并按时间戳降序排序。
SELECT * FROM fehlerfilm group by id ORDER BY timestamp desc