将多行数据透视到单列

发布于 2024-11-15 14:03:55 字数 474 浏览 2 评论 0原文

我有一个数据库表,其中包含不同产品的类别。每个类别只有一个父类别,每个产品可以有多个类别。我需要为数据迁移构建一种面包屑导航,但无法弄清楚如何映射数据,这样我就不会进行大量查询。

数据库的设置如下:

id     category              sort     parent
1      Home                  0        0
58     Car & Truck           4        1
135    10' Wide Shelters     0        58

现在我需要像这样取回数据: Home/Car &卡车/10'宽避难所

这可以用纯 SQL 实现吗?还是需要混合 ColdFusion 才能实现?如果我可以用纯 SQL 做到这一点,那么我将如何做到这一点,如果我不能,那么 ColdFusion 会是什么样子?

I have a database table with categories for different products in it. Each category has only one parent and each product can have multiple categories. I need to build a sort of breadcrumb navigation for a data migration, but can't figure out how to map the data so that I don't have tonds of queries.

The database is setup like so:

id     category              sort     parent
1      Home                  0        0
58     Car & Truck           4        1
135    10' Wide Shelters     0        58

Now I need to get the data back like this: Home/Car & Truck/10' Wide Shelters.

Is this possible with pure SQL or do I need to mix-in ColdFusion to get it. If I can do this with pure SQL then how would I do that, and if I can't then what would the ColdFusion look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林很绿却致人迷途 2024-11-22 14:03:55

其他 RDBMS 支持解决此问题的各种方法(ANSI SQL 中的递归 with、Oracle 中的 connect by 等)。但在 MySQL 中,你几乎只剩下嵌套集

Other RDBMSes support various ways of solving this problem (recursive with in ANSI SQL, connect by in Oracle, etc). But in MySQL, you're pretty much left with nested sets.

静水深流 2024-11-22 14:03:55

使用嵌套集,按照 user349433 的建议,我能够提出这个查询“

SELECT c1.id AS id1, c1.category AS name1, c2.id AS id2, c2.category AS name2, c3.id AS id3, c3.category AS name3, c4.id AS id4, c4.category AS name4, c5.id AS id5, c5.category AS name5
FROM category c1
LEFT JOIN category AS c2 ON c2.parentid = c1.id
LEFT JOIN category AS c3 ON c3.parentid = c2.id
LEFT JOIN category AS c4 ON c4.parentid = c3.id
LEFT JOIN category AS c5 ON c5.parentid = c4.id

我的数据永远不会超过 5 层”(我通过检查 c6 并获取空值进行验证)。从这里我可以对此进行查询,

SELECT (name1 + '/' + name2 + '/' + name3 + '/' + name4 + '/' + name5) AS category
FROM getCats
WHERE
    <cfloop query="Arguments.assignments">
        (id1 = #Arguments.assignments.categoryid# OR id2 = #Arguments.assignments.categoryid# OR id3 = #Arguments.assignments.categoryid# OR id4 = #Arguments.assignments.categoryid# OR id5 = #Arguments.assignments.categoryid#)
        <cfif Arguments.assignments.currentrow IS NOT Arguments.assignments.recordCount> OR </cfif>
    </cfloop>

我已经查询了产品及其类别 id 来循环。

Using nested sets, as recommended by user349433 I was able to come up with this query

SELECT c1.id AS id1, c1.category AS name1, c2.id AS id2, c2.category AS name2, c3.id AS id3, c3.category AS name3, c4.id AS id4, c4.category AS name4, c5.id AS id5, c5.category AS name5
FROM category c1
LEFT JOIN category AS c2 ON c2.parentid = c1.id
LEFT JOIN category AS c3 ON c3.parentid = c2.id
LEFT JOIN category AS c4 ON c4.parentid = c3.id
LEFT JOIN category AS c5 ON c5.parentid = c4.id

My data never goes deeper than 5 levels (I verified by checking with c6 and getting nulls). From here I can query against this

SELECT (name1 + '/' + name2 + '/' + name3 + '/' + name4 + '/' + name5) AS category
FROM getCats
WHERE
    <cfloop query="Arguments.assignments">
        (id1 = #Arguments.assignments.categoryid# OR id2 = #Arguments.assignments.categoryid# OR id3 = #Arguments.assignments.categoryid# OR id4 = #Arguments.assignments.categoryid# OR id5 = #Arguments.assignments.categoryid#)
        <cfif Arguments.assignments.currentrow IS NOT Arguments.assignments.recordCount> OR </cfif>
    </cfloop>

I already had a query of products and their category ids to loop over.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文