从created_at命令的表中获取最后的消息

时间:2016-09-30 02:44:32

标签: mysql activerecord yii2 mariadb

也许这是一个持续不断的问题,但我需要的帮助不大。 我有一张桌子

CREATE TABLE `ticket_message` (
  `id` int(11) NOT NULL,
  `ticket_id` int(11) NOT NULL,
  `message` text COLLATE utf8_unicode_ci NOT NULL,
  `created_at` int(11) NOT NULL,
  `updated_at` int(11) NOT NULL,
  `user_flag` tinyint(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我只需要收到最后的消息, 例如:

这是:

'57', '26', 'Hello\r\n', '1474019653', '1474019653', '0'
'58', '26', 'Where is my order?', '1474019672', '1474019672', '0'
'59', '27', 'Hi', '1474019824', '1474019824', '0'
'60', '28', 'Hi\r\n', '1474019921', '1474019921', '0'
'61', '28', 'Where is my item?', '1474019933', '1474019933', '0'
'62', '26', 'Hello', '1474019983', '1474019983', '1'
'63', '26', 'Hello', '1474020069', '1474020069', '0'
'64', '26', 'fdsfdsfds', '1474020191', '1474020191', '1'
'65', '26', 'fdsfdsfdsfdsfdsfdsfs', '1474020256', '1474020256', '0'
'66', '26', 'fdsfsfsfdsfs', '1474020358', '1474020358', '0'
'67', '27', 'hello', '1474020872', '1474020872', '1'
'68', '27', 'how are you', '1474020953', '1474020953', '0'
'69', '27', 'okey', '1474020963', '1474020963', '0'
'70', '29', 'This is a test message', '1474021973', '1474021973', '0'
'71', '29', 'This is also a test message', '1474023004', '1474023004', '1'
'72', '30', 'Test  message', '1474025254', '1474025254', '0'
'73', '30', 'Test Reply Message', '1474025473', '1474025473', '1'
'74', '28', '123', '1475166801', '1475166801', '1'
'75', '27', 'foo\r\n', '1475167415', '1475167415', '1'

我只想要一个查询,也许是“原始”,也许是“yii2”,它会让我回复

  1. 测试回复消息
  2. FOO
  3. fdsfsfsfdsfs
  4. 这也是一条测试消息
  5. 123

1 个答案:

答案 0 :(得分:0)

SELECT * FROM ticket_message
    ORDER BY created_at DESC LIMIT 5;

如果框架不会让你这么说,那就对他们说。

GROUP BY不合适。)
(不需要子查询。)