MySQL:使用单行查询多列的性能

发布于 2024-12-19 11:14:56 字数 657 浏览 2 评论 0原文

我的表格简化了这样的事情......

Table Categories
cat_id   | cat_name
-------------------
1        | Baseball
2        | Hockey
3        | Football

Table Day
day_id   | day_name
--------------------
1        | Friday
2        | Saturday
3        | Sunday

Table Day_Categories
day_id    | cat_id
-------------------
1         | 1
1         | 2
2         | 2
2         | 3
3         | 1
3         | 2
3         | 3

我希望我的结果数据看起来像这样......

"Friday","Baseball, Hockey"
"Saturday","Hockey, Football"
"Sunday", "Baseball, Hockey, Football"

它不一定需要采用三行格式。我只需要相同的结果数据。

从大量数据的性能角度来看,实现这一结果的最佳方法是什么?

My tables simplified something like this..

Table Categories
cat_id   | cat_name
-------------------
1        | Baseball
2        | Hockey
3        | Football

Table Day
day_id   | day_name
--------------------
1        | Friday
2        | Saturday
3        | Sunday

Table Day_Categories
day_id    | cat_id
-------------------
1         | 1
1         | 2
2         | 2
2         | 3
3         | 1
3         | 2
3         | 3

And I want my resulting data to look something like this...

"Friday","Baseball, Hockey"
"Saturday","Hockey, Football"
"Sunday", "Baseball, Hockey, Football"

It doesn't necessary need to be in three row format. I just need the same resulting data.

From a performance prospective with lots of data what's likely the best way to achieve this result?

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

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

发布评论

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

评论(1

江城子 2024-12-26 11:14:56

请试试这个

SELECT day_name, GROUP_CONCAT ( cat_name ) as categories 
FROM Days as d
LEFT JOIN Days_Categories as dnc ON d.day_id = dnc.day_id 
LEFT JOIN Categories as c ON c.cat_id = dnc.cat_id 
GROUP BY ( dnc.day_id)

Please try this one

SELECT day_name, GROUP_CONCAT ( cat_name ) as categories 
FROM Days as d
LEFT JOIN Days_Categories as dnc ON d.day_id = dnc.day_id 
LEFT JOIN Categories as c ON c.cat_id = dnc.cat_id 
GROUP BY ( dnc.day_id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文