Home > Mobile >  How to display and link two sql tables?
How to display and link two sql tables?

Time:10-06

I'm trying to display genres from a "Genre" Table in SQL to my main table I found a way to do it with one item but I cannot find a way to do it with multiple genres

conn = sqlite3.connect("ShowInformation - Copy.db")
c = conn.cursor()
c.execute('''SELECT name, GenreName, GenreName, Synopsis, EPCount, AgeRating, Score, Studio, Popularity
  FROM Genres AS g
  JOIN Animes AS a
    ON a.GenreID1 AND a.GenreID2 = g.ID;''')
item = c.fetchall()
print (item[0])
print (item)

I got an error from this and I don't know how to fix it.

My SQL database: enter image description here enter image description here

CodePudding user response:

Your ON clause is a bit odd looking for me. You seem to have missed part of the statement on the left of the AND. Did you mean to put in the below?

ON a.GenreID1 = g.ID AND a.GenreID2 = g.ID

Sharing the error message always helps people help you.

CodePudding user response:

Do a LEFT JOIN for each genre:

SELECT a.*, g1.GenreName, g2.GenreName, ...
FROM Animes AS a
LEFT JOIN Genres AS g1 ON a.GenreID1 = g1.ID
LEFT JOIN Genres AS g2 ON a.GenreID2 = g2.ID
...
  • Related