首页 > 解决方案 > SQL查询艺术家、专辑和歌曲

问题描述

我有个问题...

在 MS Access 中,我有一个表单,您可以在其中搜索艺术家、专辑和歌曲。现在我想查询搜索结果,但不确定是否可以。这是我的数据库的示例: 在此处输入图像描述

现在我想搜索艺术家、专辑和歌曲。当它找到艺术家时,我希望专辑和编号列为空。当它创建一个专辑时,我想填写艺术家和专辑列,数字行必须为空。当它找到一首歌时,我希望填满所有 3 列

这有可能吗?

标签: sqlms-access

解决方案


您可以使用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

推荐阅读