我有一个数据库在这个数据库中,一个季节记录了多个节目,并且在节目中执行/录制了多首歌曲,还有一首歌曲有多个音乐键,音乐家和乐队成员使用乐器。
以下是我的数据库的规范化表格
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)
注意:一首歌由Musician,Band执行,所以歌曲主键是音乐家和乐队作为外键,Musician和Band使用乐器录制歌曲。乐队有乐队成员。
我的查询是:对于特定记录,列出用于制作记录的所有乐器和主要音符(音乐键)?
注意:我的记录是我的Program_id,因为每个节目都是在一个季节录制的。
现在这是我要加入的查询:
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;
使用
加入此查询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;
这两个查询都给了我正确的结果,但另外,我不知道如何加入它们并通过一个查询打印结果
注意:歌曲与songs_performed,musician和band table相关联。
任何人都可以加入这些查询吗? 如果有人对我的设计有任何疑惑,请随时问。
答案 0 :(得分:1)
代表OP发布答案:
问题已经解决了。我刚刚在两个查询之间写了Union All
并且它有效。