I am trying to write an SQL query to return the name of the person who adopted the cat 'seashell'
I have a adopters
table with people who will or have adopted an animal.
pets=# SELECT * FROM adopters;
id | first_name | last_name | address | phone_number
----+------------+-----------+----------------------+--------------
1 | jack | smith | 1 ave | 123-123-1234
2 | james | dude | 2 ave | 221-234-4444
3 | Forrest | Stone | 3 lane | 234-667-6543
4 | craig | list | 44 bulvard | 555-444-3243
5 | kap | seven | no team | 999-000-0909
6 | Miles | Garrett | First energy stadium | 123-555-2424
(6 rows)
A cats
table with the cats on file
pets=# SELECT * FROM cats;
id | name | gender | age | intake_date | adoption_date
----+----------+--------+-----+---------------------+---------------------
1 | Mushi | M | 1 | 2016-01-09 00:00:00 | 2016-03-22 00:00:00
2 | Seashell | F | 7 | 2016-01-09 00:00:00 |
3 | Azul | M | 3 | 2016-01-11 00:00:00 | 2016-04-17 00:00:00
4 | Victoire | M | 7 | 2016-01-11 00:00:00 | 2016-09-01 00:00:00
5 | Nala | F | 1 | 2016-01-12 00:00:00 |
(5 rows)
And an adoptions
table with a record of all the adoptions that have taken place.
pets=# SELECT * FROM adoptions;
id | adopters | cat | dog | fee | date
----+----------+-----+-------+-------+------------
1 | 1 | | 10001 | 10.50 | 2017-11-22
2 | 2 | | 10007 | 10.50 | 2017-11-22
3 | 3 | 5 | | 10.50 | 2017-11-22
4 | 5 | 2 | | 10.50 | 2016-11-22
(4 rows)
I am having trouble with the concept of joining three tables and even if you don't have a direct answer to the problem I would greatly appreciate being pointed in the direction of a resource that would help.
This should work:
SELECT p.first_name, p.last_name
FROM adoptions a
JOIN cats c on c.id = a.cat
JOIN adopters p on p.id = a.adopters
WHERE c.name = 'seashell'
Try this
SELECT adopters.first_name, adopters.last_name
FROM adopters
INNER JOIN adoptions on adopters.id = adoptions.adopters
INNER JOIN cats on cats.id = adoptions.cats
WHERE cats.id = 2
TRY:
SELECT
pers.first_name,
pers.last_name
FROM
cats ct
JOIN
adoptions adop ON (ct.id = adop.id)
JOIN
adopters pers ON (adop.adopters = pers.id)
WHERE
ct.name = "Seashell ";
SELECT a.first_name, a.last_name
FROM adopters a, cats c, adoptions ad
WHERE a.id = ad.adopters AND ad.cat = c.id
AND c.name = "Seashell";
Please Try This
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.