SQL:使用外键从另一个表中选择数据

时间:2017-12-12 21:26:09

标签: sql

我是SQL的新手,为了学习,我试图在SQL中进行一些查询。所以我做了这两个表:

account(id, email, password)
address(id, city, street, c_id(foreign key for account))

现在我想选择一个城市的所有电子邮件。贝娄我试过的。

SELECT email
FROM account
WHERE id=(SELECT c_id
          FROM address
          WHERE city='new york');

此查询无效!

1 个答案:

答案 0 :(得分:0)

你可以用这个

SELECT email
FROM account
WHERE id in (SELECT c_id
             FROM address
             WHERE city='new york');

SELECT email
FROM account 
    JOIN address ON account.id = address.c_id
WHERE address.city='new york'