views:

96

answers:

1

I have a problem querying multiple tables in MySQL and am currently at my wits end.

I have unique IDs 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?

+2  A: 

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
Gushiken
Thanks for the answer, but this query still produces cartesian, any other ideas please?
Speedy
hm.. then just tell us what u exactly expect as the result... As far as I can tell, these querys should not produce an certesian product...
Gushiken