gpt4 book ai didi

sqlite - SQLITE在表之间选择

转载 作者:行者123 更新时间:2023-12-03 15:32:08 25 4
gpt4 key购买 nike

这显然是一项家庭作业,但我想知道如何进行这项工作,以便我可以完成其余的工作。
“编写一个SQL select命令以产生由奥斯卡·哈默斯坦创作的所有音乐剧的标题。”
我尝试“从音乐剧中选择标题,作者在authors.name =“ Oscar Hammerstein”中;“
它返回音乐剧的所有标题。
我究竟做错了什么?

最佳答案

该查询正在使用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


(此外,即使SQLite接受 'ANSI SQL quotes',我也建议使用 "MySQL quotes"。)

关于sqlite - SQLITE在表之间选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com