使用 SQL 联接缩小结果范围?
Narrow down results with SQL joins?
我是 SQL 的新手,我有三个表:
ALBUMS
album_id | album_name | artist_id | category_id
CATEGORIES
category_id | category_name
ARTISTS
artist_id | artist_name
给定一个 category_name
,我怎样才能得到 category_id, album_id, album_name, artist_id, artist_name
的结果集?
我已经尝试了 JOIN 的多种变体,但我总是以更多结果结束,而不是缩小结果范围。这可能吗?
如果你想要类别 "abc" 这样做
Select a.category_id, a.album_id, c.album_name, a.artist_id, a.artist_name
from ALBUMS a
join CATEGORIES b on a.category_id=b.category_id
join ARTISTS c on a.artist_id =c.artist_id
where c.Categoryname='abc'
select ALBUMS.category_id
, ALBUMS.album_id
, ALBUMS.album_name
, ALBUMS.artist_id
, ARTISTS.artist_name
from ALBUMS
inner join ARTISTS on ALBUMS.artist_id = ARTISTS.artist_id
inner join CATEGORIES on ALBUMS.category_id = CATEGORIES .category_id
where CATEGORIES.category_name = 'Your category name'
我是 SQL 的新手,我有三个表:
ALBUMS
album_id | album_name | artist_id | category_id
CATEGORIES
category_id | category_name
ARTISTS
artist_id | artist_name
给定一个 category_name
,我怎样才能得到 category_id, album_id, album_name, artist_id, artist_name
的结果集?
我已经尝试了 JOIN 的多种变体,但我总是以更多结果结束,而不是缩小结果范围。这可能吗?
如果你想要类别 "abc" 这样做
Select a.category_id, a.album_id, c.album_name, a.artist_id, a.artist_name
from ALBUMS a
join CATEGORIES b on a.category_id=b.category_id
join ARTISTS c on a.artist_id =c.artist_id
where c.Categoryname='abc'
select ALBUMS.category_id
, ALBUMS.album_id
, ALBUMS.album_name
, ALBUMS.artist_id
, ARTISTS.artist_name
from ALBUMS
inner join ARTISTS on ALBUMS.artist_id = ARTISTS.artist_id
inner join CATEGORIES on ALBUMS.category_id = CATEGORIES .category_id
where CATEGORIES.category_name = 'Your category name'