对艺术家,专辑和歌曲的SQL查询

时间:2019-11-10 09:26:03

标签: sql ms-access

我有问题...

在MS Access中,我有一个表格,您可以在其中搜索艺术家,专辑和歌曲。现在,我想查询搜索结果,但不确定是否可行。这是我的数据库的示例: enter image description here

现在,我要搜索歌手,专辑和歌曲。找到艺术家后,我希望专辑和编号栏为空。当找到专辑时,我要填写艺术家和专辑列,并且数字行必须为空。当它找到一首歌时,我希望所有3列都填满

这有可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用union all

select id as artist_id, name as artist_name,
       null as album_id, null as album_name,
       null as track_id, null as track_name
from artists
where . . .    -- your conditions here
union all
select null as artist_id, null as artist_name,
       id as album_id, name as album_name,
       null as track_id, null as track_name
from albums
where . . .    -- your conditions here
union all
select null as artist_id, null as artist_name,
       null as album_id, null as album_name,
       id as track_id, name as track_name
from tracks
where . . .    -- your conditions here