简体   繁体   中英

SQL: Selecting data from another table using foreign keys

I'm newbie in SQL and for purpose of learning I'm trying to make some queries in SQL. So i made these two tables:

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

Now I would like to select all emails given a city. Bellow what i tried.

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

This query is not working!

you can use this

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM