创建临时表 CONCAT GROUPS 被放置到它们自己的列中?

发布于 2024-11-15 13:29:26 字数 932 浏览 2 评论 0原文

我是一个 MYsql 新手,我对如何做到这一点感到困惑。我想我必须将结果插入临时表中,但我不确定。

我想从数据库中输出一份报告,显示我收藏中的每张音乐专辑(专辑 1、专辑 2、专辑 3),并为专辑可用的每种格式(WAV、CD、磁带等)提供单独的列。我不确定如何采用 format_types 和专辑之间的多对一关系并输出一个报告,其中每种格式都有一列,每个专辑只有一行。

我有三个表(专辑、格式类型和出现次数)

albums 
id      uuid
1       d2ec45e9-5fcc
2       949ebf32-8c95
3       98c7cc7e-ebe4
format_types
id  name
1   cassette
2   CD
3   DAT
4   WAV
album_occurrences
id  album_id format_type_id
1   1        1
2   2        2
3   3        3
4   2        3
5   3        1
6   1        2
7   1        3
8   1        4
9   2        4

我想要的结果是:

albums_by_format
album_id    format_name_1   format_name_2   format_name_3   format_name_4
1           Cassette        CD              DAT             WAV
2           NULL            CD              NULL            WAV
3           Cassette        NULL            DAT             NULL

我想知道返回上述结果的最佳方法?

I'm a bit of a MYsql newbie and I'm stumped as to how to do this. I think I have to insert my results into a TEMPORARY TABLE, but I'm not sure.

I’d like to output a report from my database that shows each music album I have in my collection (album1, album2, album3) with a separate column for each format the album is available in (WAV, CD, cassette, etc). I'm not sure how to take the many-to-one relationship between format_types and album and output a report where each format has a column and each album has only one row.

I have three tables (albums, format_types, and occurrences)

albums 
id      uuid
1       d2ec45e9-5fcc
2       949ebf32-8c95
3       98c7cc7e-ebe4
format_types
id  name
1   cassette
2   CD
3   DAT
4   WAV
album_occurrences
id  album_id format_type_id
1   1        1
2   2        2
3   3        3
4   2        3
5   3        1
6   1        2
7   1        3
8   1        4
9   2        4

The result I’d like is:

albums_by_format
album_id    format_name_1   format_name_2   format_name_3   format_name_4
1           Cassette        CD              DAT             WAV
2           NULL            CD              NULL            WAV
3           Cassette        NULL            DAT             NULL

I'm wondering the best method to return a result like the above?

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

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

发布评论

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

评论(1

萌能量女王 2024-11-22 13:29:26

下面应该给你你所需要的。我说“应该”是因为我还没有测试过。

SELECT `al`.`id`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 1
    ) `format_name_1`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 2
    ) `format_name_2`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 3
    ) `format_name_3`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 4
    ) `format_name_4`
FROM
`albums` `al`;

让我知道它是否有效:-)

The below should give you what you need. I say "should" because I haven't tested it.

SELECT `al`.`id`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 1
    ) `format_name_1`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 2
    ) `format_name_2`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 3
    ) `format_name_3`,
    (
    SELECT `ft`.`name`
    FROM `format_types` `ft`
    INNER JOIN `album_occurrences` `ao` ON `ft`.`id` = `ao`.`format_type_id`
    WHERE `ao`.`album_id` = `al`.`id`
    AND `ft`.`id` = 4
    ) `format_name_4`
FROM
`albums` `al`;

Let me know if it works :-)

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