Skip to content
Advertisement

How to display and link two sql tables?

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

Advertisement

Answer

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
...
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement