sql - SQL查询艺术家、专辑和歌曲
问题描述
我有个问题...
在 MS Access 中,我有一个表单,您可以在其中搜索艺术家、专辑和歌曲。现在我想查询搜索结果,但不确定是否可以。这是我的数据库的示例:
现在我想搜索艺术家、专辑和歌曲。当它找到艺术家时,我希望专辑和编号列为空。当它创建一个专辑时,我想填写艺术家和专辑列,数字行必须为空。当它找到一首歌时,我希望填满所有 3 列
这有可能吗?
解决方案
您可以使用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
推荐阅读
- xamarin - Xamarin.Forms 项目中的“在当前上下文中找不到初始化组件”
- excel - 找不到变量时如何处理
- python - 如何使用 nginx 和 gunicorn 使我的 python-flask 网站 24*7 可用
- html - 如何创建这种类型的形状,里面有文字?
- javascript - javascript 函数表达式是否类似于或基于 s 表达式?
- python - 使用 lxml 在根元素之前/之后访问处理指令
- java - 如何编写 Java 代码从 excel 表中读取 2 个查询并比较它们的数据库记录?
- php - 从值中获取子字符串作为数组中的键
- sql-server - Xp_CmdShell Powershell 脚本未找到有效模块
- javascript - 有没有办法列出机器人所在的 Discord 服务器的所有 ID?