简体   繁体   English

将两个SQL查询与多个表联接

[英]Join two SQL queries with multiple tables

I have a Database In this Database a seasons has multiple programs recorded and multiple songs are performed/recorded in the program, also a song has multiple musical keys and Musician and Band members Uses Instruments. 我有一个数据库在这个数据库中,一个季节记录了多个程序,并且在该程序中执行/录制了多首歌曲,而且一首歌还具有多个音乐键,并且Musical and Band成员使用乐器。

Below are the normalized tables of my Database 以下是我数据库的规范化表

seasons -> season_id(pk), name
programs -> program_id(pk), name
programs_recorded -> p_id(pk), season_id(fk), program_id(fk)
songs -> song_id(pk), title
songs_performed -> s_id, song_id(fk), program_id(fk)
musical_keys -> song_id(fk), music_keys
musician -> musician_id(pk), name, song_id
band -> band_id(pk), song_id(fk), band_name
band_member -> member_id(pk), band_id(fk), member_name
instrument -> instrument_id, instrument_name 
musician_plays -> m_id(pk), musician_id(fk), instrument_id(fk)
member_plays -> mem_id(pk), instrument_id(fk), member_id(fk)  

Note: A song is performed in program by Musician, Band so songs primary key is in musician and band as foreign keys, also Musician and Band uses Instruments to record songs. 注意:歌曲是由Musician,Band在程序中执行的,因此歌曲的主键在音乐家和Band中作为外键,而且Musician和Band也使用乐器录制歌曲。 Band has band members. 乐队有乐队成员。

My Query is: For a specific record, list all the instruments and the key notes(musical keys) that have been used to produce the record? 我的查询是:对于特定记录,列出所有用于产生记录的乐器和键音(音乐键)?

Note: My record is my Program_id, as each program is recorded in a season. 注意:我的记录是我的Program_id,因为每个节目都是按季节记录的。

Now here are my queries which I want to join: 现在这是我想加入的查询:

SELECT
    programs.program_id,
    GROUP_CONCAT(DISTINCT instrument_name),
    GROUP_CONCAT(DISTINCT music_keys)
FROM
    seasons
    INNER JOIN programs_recorded ON seasons.season_id=programs_recorded.season_id
    INNER JOIN programs ON programs_recorded.program_ID=programs.program_ID
    INNER JOIN songs_performed ON programs.program_ID=songs_performed.program_ID
    INNER JOIN songs ON songs_performed.song_id=songs.song_id
    INNER JOIN musical_keys ON songs.song_id=musical_keys.song_id
    INNER JOIN band ON band.song_id=songs.song_id
    INNER JOIN band_member ON band_member.band_id=band.band_id
    INNER JOIN member_plays ON member_plays.member_id=band_member.member_id
    INNER JOIN instrument ON  member_plays.instrument_id=instrument.instrument_id 
WHERE
    songs_performed.program_id=12000
GROUP BY songs_performed.program_id; 

Join this query with 加入这个查询

SELECT
    programs.program_id,
    GROUP_CONCAT(DISTINCT instrument_name),
    GROUP_CONCAT(DISTINCT music_keys)
FROM
    seasons
    INNER JOIN programs_recorded ON seasons.season_id=programs_recorded.season_id
    INNER JOIN programs ON programs_recorded.program_ID=programs.program_ID
    INNER JOIN songs_performed ON programs.program_ID=songs_performed.program_ID
    INNER JOIN songs ON songs_performed.song_id=songs.song_id
    INNER JOIN musical_keys ON songs.song_id=musical_keys.song_id
    INNER JOIN musician ON musician.song_id=songs.song_id
    INNER JOIN musician_plays ON musician_plays.musician_id=musician.musician_id
    INNER JOIN instrument ON musician_plays.instrument_id=instrument.instrument_id
WHERE
    songs_performed.program_id=12000
GROUP BY songs_performed.program_id;

Both the queries give me the correct result but separately, I don't know how to join them and print the result through one query 这两个查询都给我正确的结果,但是分别地,我不知道如何将它们连接起来并通过一个查询打印结果

Note: Song is connected with songs_performed , musician and band table. 注意:歌曲与songs_performed,音乐家和乐队表相关。

Can Anyone join these queries? 任何人都可以加入这些查询吗? If anyone have any confusion about my design please feel free to ask. 如果有人对我的设计有任何疑问,请随时提出。

Posted as an answer on behalf of the OP: 代表OP发布作为答案:

The problem's solved. 问题解决了。 I just wrote Union All between the two queries and it worked. 我只是在两个查询之间编写了Union All ,它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM