Home Trying to join three tables to get a specific result
Reply: 4

Trying to join three tables to get a specific result

SinGar
1#
SinGar Published in 2017-12-07 20:44:02Z

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.

dimwittedanimal
2#
dimwittedanimal Reply to 2017-12-07 20:51:15Z

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
DDeMartini
3#
DDeMartini Reply to 2017-12-07 20:51:26Z

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 ";
isaace
4#
isaace Reply to 2017-12-07 21:11:57Z

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'
PHP Geek
5#
PHP Geek Reply to 2017-12-08 04:42:56Z
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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303242 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO