MySQL 5.7 - 使用最早创建的 child 更新 parent 记录
MySQL 5.7 - Update parent records with oldest created child
我的目标是创建一个 UPDATE
语句来更新 parent 记录的 default_page_id
成为最早 child 创建的记录。
parent/child是2级max/deep受UI限制; parent_id
列表示记录的 parent,root
的 type
值进一步简化了 root/parents 的内容。
我有一个带 DDL/DML 的 dbfiddle:https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=994d9bf3b20b3bbab21547d4334fc0e7
最终我的 SELECT
会变成我的 UPDATE
is here but uses MIN(id)
wouldn't be feasible as id
is a char(36)
因此 id
不会自动递增;我已尝试使用 MIN(created)
,但缺少对 GROUP BY 的必要更新以解决此问题,希望能多多关注!
SELECT
*
FROM
(
SELECT
test_pages.id
FROM
test_pages
WHERE
test_pages.default_page_id is null
and test_pages.type = "root"
) as a
LEFT JOIN(
SELECT
parent_id ,
MIN(id) AS id
from
test_pages
WHERE
test_pages.type = "container"
GROUP BY
parent_id
) as b ON b.parent_id = a.id
SQL (SELECT):
如果我对问题的理解正确,以下 sql 可能会有所帮助:
SELECT
r.id,
r.default_page_id,
r.type,
r.title,
c.parent_id AS container_parent_id,
c.id AS container_id
FROM
test_pages AS r
LEFT JOIN (
SELECT
parent_id,
id
FROM
test_pages
WHERE
type = "container"
AND created = (
SELECT
MIN(created)
FROM
test_pages AS tp
WHERE
type = "container"
AND test_pages.parent_id = tp.parent_id
)
) AS c ON r.id = c.parent_id
WHERE
type = "root";
SQL(更新):
更新sql如下:
UPDATE
test_pages AS t
SET
t.default_page_id = (
SELECT
id
FROM
(
SELECT
parent_id,
id
FROM
test_pages
WHERE
type = "container"
AND created = (
SELECT
MIN(created)
FROM
test_pages AS tp
WHERE
type = "container"
AND test_pages.parent_id = tp.parent_id
)
) AS c
WHERE
t.id = c.parent_id
);
输出:
更新后,test_pages
如下所示 table:
id
version_id
parent_id
default_page_id
type
title
created
modified
jf2039jf932032f
2
abc1212312efff
nan
container
Child Page 2
2022-02-23 23:25:41
nan
7eajiefaeiaeaeleailje
2
nan
8efjaliejfaelfjael
root
Root Page A
nan
nan
8efjaliejfaelfjael
2
7eajiefaeiaeaeleailje
nan
container
Child Page Again 1
2022-02-04 23:26:01
nan
abc1212312efff
2
nan
ejejejejejej
root
Root Page
nan
nan
ejejejejejej
2
abc1212312efff
nan
container
Child Page 1
2022-02-03 23:26:01
nan
fefeee9f9e9fee
2
nan
nan
root
Root Page With No Children
nan
nan
jklfjealkjlfajfl
2
7eajiefaeiaeaeleailje
nan
container
Child Page Again 2
2022-02-07 23:25:41
nan
注:
如果同一父容器下有多个id
不同但created
完全相同的容器,可能会报错如下。在这种情况下,您必须编写额外的代码。所以请小心。
Query 1: Subquery returns more than 1 row
我的目标是创建一个 UPDATE
语句来更新 parent 记录的 default_page_id
成为最早 child 创建的记录。
parent/child是2级max/deep受UI限制; parent_id
列表示记录的 parent,root
的 type
值进一步简化了 root/parents 的内容。
我有一个带 DDL/DML 的 dbfiddle:https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=994d9bf3b20b3bbab21547d4334fc0e7
最终我的 SELECT
会变成我的 UPDATE
is here but uses MIN(id)
wouldn't be feasible as id
is a char(36)
因此 id
不会自动递增;我已尝试使用 MIN(created)
,但缺少对 GROUP BY 的必要更新以解决此问题,希望能多多关注!
SELECT
*
FROM
(
SELECT
test_pages.id
FROM
test_pages
WHERE
test_pages.default_page_id is null
and test_pages.type = "root"
) as a
LEFT JOIN(
SELECT
parent_id ,
MIN(id) AS id
from
test_pages
WHERE
test_pages.type = "container"
GROUP BY
parent_id
) as b ON b.parent_id = a.id
SQL (SELECT):
如果我对问题的理解正确,以下 sql 可能会有所帮助:
SELECT
r.id,
r.default_page_id,
r.type,
r.title,
c.parent_id AS container_parent_id,
c.id AS container_id
FROM
test_pages AS r
LEFT JOIN (
SELECT
parent_id,
id
FROM
test_pages
WHERE
type = "container"
AND created = (
SELECT
MIN(created)
FROM
test_pages AS tp
WHERE
type = "container"
AND test_pages.parent_id = tp.parent_id
)
) AS c ON r.id = c.parent_id
WHERE
type = "root";
SQL(更新):
更新sql如下:
UPDATE
test_pages AS t
SET
t.default_page_id = (
SELECT
id
FROM
(
SELECT
parent_id,
id
FROM
test_pages
WHERE
type = "container"
AND created = (
SELECT
MIN(created)
FROM
test_pages AS tp
WHERE
type = "container"
AND test_pages.parent_id = tp.parent_id
)
) AS c
WHERE
t.id = c.parent_id
);
输出:
更新后,test_pages
如下所示 table:
id | version_id | parent_id | default_page_id | type | title | created | modified |
---|---|---|---|---|---|---|---|
jf2039jf932032f | 2 | abc1212312efff | nan | container | Child Page 2 | 2022-02-23 23:25:41 | nan |
7eajiefaeiaeaeleailje | 2 | nan | 8efjaliejfaelfjael | root | Root Page A | nan | nan |
8efjaliejfaelfjael | 2 | 7eajiefaeiaeaeleailje | nan | container | Child Page Again 1 | 2022-02-04 23:26:01 | nan |
abc1212312efff | 2 | nan | ejejejejejej | root | Root Page | nan | nan |
ejejejejejej | 2 | abc1212312efff | nan | container | Child Page 1 | 2022-02-03 23:26:01 | nan |
fefeee9f9e9fee | 2 | nan | nan | root | Root Page With No Children | nan | nan |
jklfjealkjlfajfl | 2 | 7eajiefaeiaeaeleailje | nan | container | Child Page Again 2 | 2022-02-07 23:25:41 | nan |
注:
如果同一父容器下有多个id
不同但created
完全相同的容器,可能会报错如下。在这种情况下,您必须编写额外的代码。所以请小心。
Query 1: Subquery returns more than 1 row