Męczę się z tym zapytaniem już jakiś czas. Nie mogę poradzić sobie z wyświetleniem ostatniego wpisu dla każdego użytkownika. Jak ułożyć selecta aby otrzymać te rekordy? Wynikiem zapytania ma być rekord o id 9 i 13
Dana jest tabela user_log. Napisz jedno zapytanie SQL, które zaznaczy notify_admin na 1 ostatni wpis dla każdego użytkownika z action_type = abuse.
+----+---------+-------------+--------------+---------------------+
| id | user_id | action_type | notify_admin | saved |
+----+---------+-------------+--------------+---------------------+
| 1 | 1 | login | 0 | 2015-11-02 12:13:14 |
| 2 | 1 | logout | 0 | 2015-11-02 13:12:11 |
| 3 | 1 | abuse | 0 | 2016-01-03 14:10:02 |
| 4 | 2 | abuse | 0 | 2016-01-04 17:47:03 |
| 5 | 2 | login | 0 | 2016-01-04 18:11:55 |
| 6 | 1 | abuse | 0 | 2016-01-04 18:23:57 |
| 7 | 1 | abuse | 0 | 2016-01-04 18:24:23 |
| 8 | 2 | logout | 0 | 2016-01-04 18:25:24 |
| 9 | 1 | abuse | 0 | 2016-01-04 18:25:32 |
| 10 | 1 | login | 0 | 2016-01-05 21:02:59 |
| 11 | 3 | login | 0 | 2016-01-05 21:28:43 |
| 12 | 3 | logout | 0 | 2016-01-05 21:52:01 |
| 13 | 2 | abuse | 0 | 2016-01-05 22:00:35 |
| 14 | 1 | logout | 0 | 2016-01-05 22:12:09 |
+----+---------+-------------+--------------+---------------------+
CREATE TABLE IF NOT EXISTS `user_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`action_type` enum('login','logout','abuse') CHARACTER SET latin1 NOT NULL,
`notify_admin` tinyint(1) NOT NULL DEFAULT '0',
`saved` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15;
INSERT INTO `user_log` (`id`, `user_id`, `action_type`, `notify_admin`, `saved`) VALUES
(1, 1, 'login', 0, '2015-11-02 12:13:14'),
(2, 1, 'logout', 0, '2015-11-02 13:12:11'),
(3, 1, 'abuse', 0, '2016-01-03 14:10:02'),
(4, 2, 'abuse', 0, '2016-01-04 17:47:03'),
(5, 2, 'login', 0, '2016-01-04 18:11:55'),
(6, 1, 'abuse', 0, '2016-01-04 18:23:57'),
(7, 1, 'abuse', 0, '2016-01-04 18:24:23'),
(8, 2, 'logout', 0, '2016-01-04 18:25:24'),
(9, 1, 'abuse', 0, '2016-01-04 18:25:32'),
(10, 1, 'login', 0, '2016-01-05 21:02:59'),
(11, 3, 'login', 0, '2016-01-05 21:28:43'),
(12, 3, 'logout', 0, '2016-01-05 21:52:01'),
(13, 2, 'abuse', 0, '2016-01-05 22:00:35'),
(14, 1, 'logout', 0, '2016-01-05 22:12:09');