MySQL Multitable Query Problem
-
05-09-2019 - |
Question
I have a problem querying multiple tables in MySQL
and am currently at my wits end.
I have unique ID
s in each table, and am using an INNER JOIN
to combine them; I am quite new to SQL
and this may be the wrong method, hence me posting here:
Query:
SELECT res.UserID, res.QuizID, res.QuizResult, u.UserID, u.UserLogin, q.QuizID, q.QuizName, q.QuizFile
FROM results res
INNER JOIN users u ON (res.UserID = u.UserID)
INNER JOIN quiz q ON (res.QuizID = q.QuizID)
Table users
contains
UserID
UserLogin
UserPass
Table quiz
contains
QuizID
QuizName
QuizFile
Table results
contains
UserID
QuizID
QuizResults
Can anybody please tell me why I am getting a cartesian product, and how I should go about fixing it?
Solution
Try this:
SELECT res.UserID, res.QuizID, res.QuizResult, u.UserID, u.UserLogin, q.QuizID, q.QuizName, q.QuizFile FROM results as res INNER JOIN users as u ON res.UserID = u.UserID INNER JOIN quiz as q ON res.QuizID = q.QuizID
A different way to realize something like an INNER JOIN is this:+
SELECT res.UserID, res.QuizID, res.QuizResult, u.UserID, u.UserLogin, q.QuizID, q.QuizName, q.QuizFile FROM results as res,users as u ,quiz as q WHERE res.UserID = u.UserID AND res.QuizID = q.QuizID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow