Question

I have 3 tables (archive has many sections, section (may) belong to many archives):

  • archive

    • id PK
    • description
  • archive_to_section

    • archive_id PK FK
    • section_id PK FK
  • section

    • id PK
    • description

What would the SQL look like to list all the sections that belong a certain archive id?

I am just learning SQL. From what I've read it sounds like I would need a join, or union? FYI I'm using postgres.


[Edit] This is the answer from gdean2323 written without aliases:

SELECT section.* 
FROM section 
INNER JOIN archive_to_section 
ON section.id = archive_to_section.section_id 
WHERE archive_to_section.archive_id = $this_archive_id
Was it helpful?

Solution

SELECT s.* 
FROM section s INNER JOIN archive_to_section ats ON s.id = ats.section_id 
WHERE ats.archive_id = 1

OTHER TIPS

SELECT s.*
FROM archive_to_section ats
  INNER JOIN section s ON s.id=ats.section_id
WHERE ats.archive_id= @archiveID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top