gpt4 book ai didi

sql - 具有多个表的SQL select语句

转载 作者:行者123 更新时间:2023-12-04 01:22:29 25 4
gpt4 key购买 nike

给出以下两个表:

Person table 
id (pk)
first
middle
last
age

Address table
id(pk)
person_id (fk person.id)
street
city
state
zip


如何创建一个SQL语句,该语句返回邮政编码为97229的人们的所有信息?

最佳答案

Select * from people p, address a where  p.id = a.person_id and a.zip='97229';


或者,您必须使用 JOIN尝试使用,这是一种更有效,更好的方法,因为下面的注释中的Gordon Linoff还说您需要学习这一点。

SELECT p.*, a.street, a.city FROM persons AS p
JOIN address AS a ON p.id = a.person_id
WHERE a.zip = '97299';


在这里 p.*表示它将显示PERSONS表的所有列。

关于sql - 具有多个表的SQL select语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12901614/

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