SQL Join 4 tables display select columns with row data

77 Views Asked by At

I am trying to join 4 tables using SQL. The tables are

  • "Article" (Primary key "a_id", "title", "au_id", "i_id")
  • "Author" (Primary key "au_id", "name")
  • "Issue" (Primary key "i_id", "j_id", "number")
  • "Journal" (Primary key "j_id", "title")

I want to display article title, author, journal and issue. Atm it only displays author and issue. However the data seems to be in the row, just not showing. Current code:

const sql = "SELECT a.a_id, a.title, j.title, iss.number, au.name
FROM Article AS a 
INNER JOIN Author AS au ON a.au_id = au.au_id 
INNER JOIN Issue AS iss ON a.i_id = iss.i_id 
INNER JOIN Journal AS j ON iss.j_id = j.j_id";
1

There are 1 best solutions below

1
On

Ensure your data is properly structured to deliver the result you want

SELECT a.a_id As Articleid
a.title AS ArticleTitle, 
au.name AS AuthorName, 
j.title AS JournalTitle, 
iss.number AS IssueNumber
FROM Article AS a
 INNER JOIN Author AS au 
 ON a.au_id = au.au_id
  INNER JOIN Issue AS iss 
  ON a.i_id = iss.i_id
   INNER JOIN Journal AS j 
   ON iss.j_id = j.j_id;