Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL在最后一个数据库上进行排序和分组_Mysql_Sql_Group By_Sql Order By - Fatal编程技术网

MySQL在最后一个数据库上进行排序和分组

MySQL在最后一个数据库上进行排序和分组,mysql,sql,group-by,sql-order-by,Mysql,Sql,Group By,Sql Order By,您好,我需要有关MYSQL查询的帮助 我有一张桌子 id | tn | title | customer_id |create_time | comment | 1 | 1342 | sample1 | customer1 | 2012-01-01 | hello world | 2 | 1342 | sample1 | customer1 | 2012-01-02 | hello world | 3 |

您好,我需要有关MYSQL查询的帮助

我有一张桌子

id | tn   | title    | customer_id |create_time | comment              |
1  | 1342 | sample1  | customer1   | 2012-01-01 | hello world          |
2  | 1342 | sample1  | customer1   | 2012-01-02 | hello world          |
3  | 1342 | sample1  | customer1   | 2012-01-03 | hello new world      |
4  | 3362 | sample2  | customer1   | 2012-01-02 | good bye world       |
5  | 3362 | sample2  | customer1   | 2012-01-03 | good bye world       |
6  | 3362 | sample2  | customer1   | 2012-01-04 | good bye world       |
7  | 3362 | sample2  | customer1   | 2012-01-05 | good bye new world   |
当我按tn分组时,我采取了

1  | 1342 | sample1  | customer1   | 2012-01-01 | hello world          |
4  | 3362 | sample2  | customer1   | 2012-01-02 | good bye world       |
但我需要一个

3  | 1342 | sample1  | customer1   | 2012-01-03 | hello new world      |
7  | 3362 | sample2  | customer1   | 2012-01-05 | good bye new world   |
这就像用最大id或最大创建时间按tn分组一样

我该怎么做?谢谢

试试这个

SELECT t2.* FROM
(SELECT MAX(id) AS id,tn FROM my_table GROUP BY tn) AS t1
LEFT JOIN my_table AS t2 USING(id)
Select t.* from 
table t right join
(Select max(id) as max_id from table group by tn) t1 on (t.id=t1.max_id)
试试这个:

mysql> select * from ( select * from tbl2 tn order by id desc ) t group by tn;
+------+------+---------+-------------+-------------+--------------------+
| id   | tn   | title   | customer_id | create_time | comment            |
+------+------+---------+-------------+-------------+--------------------+
|    3 | 1342 | sample1 | customer1   | 2012-01-03  | hello new world    |
|    7 | 3362 | sample2 | customer1   | 2012-01-05  | good bye new world |
+------+------+---------+-------------+-------------+--------------------+
2 rows in set (0.02 sec)

按id添加订单描述限额1您能提供您的实际查询吗?