这显然是家庭作业,但我想知道如何做这个,以便我可以做其余的事情。 “写一个SQL select命令来制作奥斯卡·汉默斯坦所创作的所有音乐剧的标题。” 我尝试“从音乐剧中选择标题,作者在哪里authors.name =”Oscar Hammerstein“;” 它返回音乐剧的所有标题。 我做错了什么?
答案 0 :(得分:1)
查询使用CROSS JOIN aka CARTESIAN PRODUCT。
r1, r2
运算符与编写r1 CROSS JOIN r2
的相同,如下所示:
select title
from musicals CROSS JOIN authors
where authors.name="Oscar Hammerstein"
要么增加WHERE的特异性,要么我的建议使用INNER [Equi-]JOIN。
select title
from musicals
JOIN authors -- just JOIN means INNER JOIN
ON musicals.author = author.name -- join on related data, adjust to schema
where authors.name = "Oscar Hammerstein"
相同的规则适用于many-many joining table的查询。在评论中使用模式:
select m.title
from authors_musicals am -- from all authors/musical pairs
-- as opposed to authors CROSS JOIN musicals, the M-M joining table
-- only has the actual author/musical pair combinations
join authors a -- choose the author
on a.id = am.author_id
join musicals m -- and the musical
on m.id = am.musical_id
where a.name = 'Oscar Hammerstein' -- where the author has a given name
(另外,我建议使用'ANSI SQL quotes'
,即使SQLite接受"MySQL quotes"
。)