Answer the question
In order to leave comments, you need to log in
What is the best way to write a SQL query spanning multiple tables?
Hey! There are the following tables:
_________________________________________________________________________________________________________________________________
people - people
planet - planets
planet_resource - resources that are present on planets
people_planet - comparison of planets and people who have access to
them planet_resource_person - comparison of resources of planets and people who have access to them
As can be seen from the tables , several people can have access to the same planet, but different access to resources is possible, including no access to resources at all.
Attention, question)How to get a list of planets for a certain person on which he has access to at least one resource? For example, Victor has access to Venus and Earth, but at the same time, access to resources is only available on Venus - that is, the result of the query should be a row with id=2 from the planet table.
Answer the question
In order to leave comments, you need to log in
It is necessary not only to link, but also to isolate unique data without duplication, the DISTINCT
instruction will help here :
SELECT DISTINCT
planet.*
FROM
planet
INNER JOIN
planet_resource
ON
planet_resource.planet_id = planet.id
INNER JOIN
planet_resource_person
ON
planet_resource_person.resource_id = planet_resource.id
INNER JOIN
people_planet
ON
people_planet.people_id = planet_resource_person.person_id
AND people_planet.planet_id = planet.id
WHERE
people_planet.people_id = ID нужного человека
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question