有一个包含 5 列的表:id
, data
, category
, time
, page
。我想获取id
>given id
和category
=的行given category
,将它们分组page
,从每个页面中获取第一个按排序的页面time
并减少page
该行中的列。(time
不是唯一的)
我设法创建了这个查询并且它有效,但我想知道是否有更好的解决方案。我在选择适当的问题标题时也遇到了问题,所以如果您认为这不完全是应该的,请随时纠正我。
UPDATE `content` AS TABLE1
INNER JOIN (SELECT MIN(TABLE2.`id`) AS ID, TABLE2.`page` FROM `content` AS TABLE2
INNER JOIN (
SELECT DISTINCT `page`, MIN(`time`) AS MINTIME
FROM `content`
WHERE (`page` > (SELECT `page` FROM `content` WHERE `id` = ?) AND `category` = ?)
GROUP BY `page`) AS TABLE3
ON TABLE2.`page` = TABLE3.`page` AND TABLE2.`time` = TABLE3.MINTIME
GROUP BY TABLE2.`page`) AS TABLE4
ON TABLE1.`id` = TABLE4.ID
SET TABLE1.`page` = TABLE1.`page` - 1