我正在尝试一次查询多个表。假设我有一个名为PRESCHOOLERS的表,我有一个名为FAVORITE_GOOEY_TREATS的表,在PRESCHOOLERS表中引用了一个外键列,引用了FAVORITE GOOEY TREAT的id字段。如果我想获得一份带有他们的名字和他们最喜欢的零食的学龄前儿童名单,我该怎么办?我的意思是:
first_name | treat
john | fudge
sally | ice-cream
这是我正在尝试的内容,但我在where部分有语法错误。
SELECT PRESCHOOLERS.first_name, FAVORITE_GOOEY_TREATS.name as treat
FROM PRESCHOOLERS, FAVORITE_GOOEY_TREATS
WHERE PRESCHOOLERS.favorite_treat = FAVORITE_GOOEY_TREATS.id and PRESCHOOLERS.age>15;
据我所知,这种事情可以通过sql标准,但sqlite3不太喜欢它。有人能指出一些类似查询的例子吗?
答案 0 :(得分:12)
尝试
SELECT PRESCHOOLERS.first_name, FAVORITE_GOOEY_TREATS.name as treat
FROM PRESCHOOLERS
JOIN FAVORITE_GOOEY_TREATS ON PRESCHOOLERS.favorite_treat = FAVORITE_GOOEY_TREATS.id
WHERE PRESCHOOLERS.age > 15;