0

我有一张这样的桌子:

+------+-----------------------------+---------+
| date |           test              | number  |
+------+-----------------------------+---------+
| 12.1 | hello!                      | 10      |
| 12.1 | hello and welcome!          | 15      |
| 12.2 | come here!                  | 20      |
| 12.2 | come here and do something! | 10      |
+------+-----------------------------+---------+

12.1带有日期和日期的行在12.2不同时间插入,我可以选择最新的插入吗?结果:

+------+-----------------------------+---------+
| date |           test              | number  |
+------+-----------------------------+---------+
| 12.1 | hello and welcome!          | 15      |
| 12.2 | come here and do something! | 10      |
+------+-----------------------------+---------+
4

4 回答 4

0

你的表有一个 auto_increment id?

如果你想要表中所有行的最后一行

SELECT * FROM table ORDER BY id DESC LIMIT 1;

于 2013-04-11T02:04:46.117 回答
0

我认为您正在尝试为每个日期选择最新的测试。尝试这个:

SELECT tb2.date, tb1.test, tb1.number 
       FROM (SELECT MAX(number) as number, date FROM tb1 GROUP BY date) AS tb2, tb1
       WHERE tb1.date = tb2.date
于 2013-04-11T02:09:05.147 回答
0

我认为这个 sql 可以帮助你

SELECT tb.date, tb.test, tb.number
FROM 
(
    SELECT DISTINCT date, test, number
    FROM test
    ORDER BY date DESC
) AS tb
GROUP BY tb.date

表如:

CREATE TABLE IF NOT EXISTS `test` (
`date` varchar(32) NOT NULL,
`test` varchar(64) NOT NULL,
`number` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


INSERT INTO `test` (`date`, `test`, `number`) VALUES
('12.1', 'hello!', 10),
('12.1', ' hello and welcome!', 15),
('12.2', 'come here!', 20),
('12.2', 'come here and do something!', 10);
于 2013-04-11T02:43:32.550 回答
0

如果您想创建一个新字段created_at with datatype (timestamp) and ORDER BY created_at DESC ,或者您可以field id使用ORDER BY id DESC.

于 2013-04-11T02:47:43.803 回答