gpt4 book ai didi

MySQL 查询 - 使用 URL 名称识别数据,其中数据被组织成层次结构

转载 作者:行者123 更新时间:2023-11-30 23:51:32 25 4
gpt4 key购买 nike

我有一个名为 content 的 mysql 表,它存储内容管理系统的内容数据。

注意:所有内容都使用父 ID 列组织到层次结构中。

+----+------------+-----------------+--------+
| id | slug | content_type_id | parent |
+----+------------+-----------------+--------+
| 1 | portfolio | 5 | 0 |
| 2 | about-us | 1 | 0 |
| 3 | find-us | 1 | 0 |
| 4 | contact-us | 1 | 2 |
| 5 | find-us | 1 | 4 |
+----+------------+-----------------+--------+

我需要一个查询来根据 slug 名称选择表中的正确行。问题在于 slug 具有相同的名称。

我有两条可能的路径,用户可以访问:

/find-us/

/about-us/contact-us/find-us/

我能想到一个解决方案:

这是用完整路径创建另一列:

full_path
--------
/portfolio/
/about-us/
/find-us/
/about-us/contact-us/
/about-us/contact-us/find-us/

但是我可以使用任何一种聪明的方法来选择正确的行。我不确定创建另一个具有完整路径名的列是否是个好主意(因为它们有可能发生变化),就我个人而言,我只想将其用作最后的手段。

谢谢。

最佳答案

如果您的 DBMS 支持递归查询,这将是可能的:

DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;

CREATE TABLE tmp.webmeuk
( id INTEGER NOT NULL PRIMARY KEY
, slug VARCHAR
, content_type_id INTEGER NOT NULL
, parent_id INTEGER REFERENCES tmp.webmeuk(id)
);
INSERT INTO tmp.webmeuk( id , slug, content_type_id , parent_id )
VALUES( 0 , 'HTTP://pr0n.mysite.xx', 5 , NULL )
, ( 1 , 'portfolio', 5 , 0 )
, ( 2 , 'about-us', 1 , 0 )
, ( 3 , 'find-us', 1 , 0 )
, ( 4 , 'contact-us', 1 , 2 )
, ( 5 , 'find-us', 1 , 4 )
;

-- a room with a view
CREATE VIEW tmp.reteview AS (
WITH RECURSIVE xx AS (
SELECT w0.id AS id
, w0.slug AS slug
, w0.content_type_id AS content_type_id
, w0.slug AS fullpath
FROM tmp.webmeuk w0
WHERE w0.parent_id IS NULL
UNION
SELECT w1.id AS id
, w1.slug AS slug
, w1.content_type_id AS content_type_id
, xx.fullpath || '/'::text || w1.slug AS fullpath
FROM tmp.webmeuk w1, xx
WHERE w1.parent_id = xx.id
)
SELECT * FROM xx
);

SELECT * FROM tmp.reteview ;

-- Change one row of data
UPDATE tmp.webmeuk
SET slug = 'what-about-us'
WHERE id = 2;

SELECT * FROM tmp.reteview ;

输出:

NOTICE:  drop cascades to 2 other objects
DETAIL: drop cascades to table tmp.webmeuk
drop cascades to view tmp.closure
DROP SCHEMA
CREATE SCHEMA
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "webmeuk_pkey" for table "webmeuk"
CREATE TABLE
INSERT 0 6
CREATE VIEW
id | slug | content_type_id | fullpath
----+-----------------------+-----------------+---------------------------------------------------
0 | HTTP://pr0n.mysite.xx | 5 | HTTP://pr0n.mysite.xx
1 | portfolio | 5 | HTTP://pr0n.mysite.xx/portfolio
2 | about-us | 1 | HTTP://pr0n.mysite.xx/about-us
3 | find-us | 1 | HTTP://pr0n.mysite.xx/find-us
4 | contact-us | 1 | HTTP://pr0n.mysite.xx/about-us/contact-us
5 | find-us | 1 | HTTP://pr0n.mysite.xx/about-us/contact-us/find-us
(6 rows)

UPDATE 1
id | slug | content_type_id | fullpath
----+-----------------------+-----------------+--------------------------------------------------------
0 | HTTP://pr0n.mysite.xx | 5 | HTTP://pr0n.mysite.xx
1 | portfolio | 5 | HTTP://pr0n.mysite.xx/portfolio
3 | find-us | 1 | HTTP://pr0n.mysite.xx/find-us
2 | what-about-us | 1 | HTTP://pr0n.mysite.xx/what-about-us
4 | contact-us | 1 | HTTP://pr0n.mysite.xx/what-about-us/contact-us
5 | find-us | 1 | HTTP://pr0n.mysite.xx/what-about-us/contact-us/find-us
(6 rows)

关于MySQL 查询 - 使用 URL 名称识别数据,其中数据被组织成层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7703199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com