簡體   English   中英

通過跳過基於 parent_id 的前 5 條記錄來選擇子行

[英]Select child rows by skipping first 5 records based on parent_id

對於以下查詢,我收到錯誤This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

SELECT * FROM `wp_dash_competition_versions` _versions
WHERE DATEDIFF(CURRENT_TIMESTAMP, _versions.created_at) > 20 AND
id NOT IN (
    SELECT id FROM `wp_dash_competition_versions` WHERE competition_id = _versions.competition_id
    ORDER BY created_at LIMIT 5
)

其他 SO 文章建議使用連接,但是連接查詢不會將限制應用於給定的 parent_id。

是否可以實現從子表中選擇行的單個查詢,跳過給定 parent_id 的前 5 行?

我主要為刪除操作開發查詢以修剪表。 我首先需要選擇以確保語句正確。

CREATE TABLE `wp_dash_competition_versions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `competition_id` bigint(20) unsigned DEFAULT NULL,
  `competition_serialised` mediumtext COLLATE utf8mb4_unicode_520_ci,
  `created_user_id` bigint(20) unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
)
SELECT _version.*
FROM `wp_dash_competition_versions` _version
INNER JOIN (
              SELECT competition_id, GROUP_CONCAT(id ORDER BY created_at DESC) _versions
              FROM `wp_dash_competition_versions` GROUP BY competition_id
) group_versions
ON _version.competition_id = group_versions.competition_id
AND FIND_IN_SET(id, _versions) > 5
AND DATEDIFF(CURRENT_TIMESTAMP, created_at) > 20

在答案https://stackoverflow.com/a/15585351/972457 中找到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM