Jump to content

[SOLVED] Help with a Query


JSHINER

Recommended Posts

SELECT 
quizzes.*, quizzes_categories.*, quizzes_taken.* 
FROM 
quizzes, quizzes_categories, quizzes_taken 
WHERE 
quizzes.category = quizzes_categories.id_cat AND quizzes.id = quizzes_taken.id_quiz AND id_user != '00123466' 
ORDER BY RAND() LIMIT 5

 

The quizzes_taken table looks like this:

 

id  |  id_quiz  |  id_user

-----------------------------

1    |      13    | 00123466

 

And I want to make it so that if there is an entry for that user (id_user) with the id_quiz (matches to quizzes.id) it does not return it.

 

Right now that query returns 0 results.

Link to comment
Share on other sites

I think I understand what you want to achieve, give this a try...

SELECT * FROM `quizzes` as q 
LEFT JOIN `quizzes_categories` as qc ON q.`category` = qc.`id_cat` 
LEFT JOIN `quizzes_taken` as qt ON q.`id` = qt.`id_quiz` 
WHERE qt.`id_user` IS NULL 
ORDER BY RAND() LIMIT 5

Link to comment
Share on other sites

Semi's example will give back all users. just add the user you are looking for.

 

This user should be in a main table that contains the users not just a table that has links to it.

 

Ray

 

Link to comment
Share on other sites

Semi's example will give back all users. just add the user you are looking for.

 

This user should be in a main table that contains the users not just a table that has links to it.

 

Ray

Oh yes, missed out the bit to narrow it down to a specific user...

SELECT * FROM `quizzes` as q 
LEFT JOIN `quizzes_categories` as qc ON q.`category` = qc.`id_cat` 
LEFT JOIN `quizzes_taken` as qt ON q.`id` = qt.`id_quiz` AND qt.`id_user`='00123466' 
WHERE qt.`id` IS NULL 
ORDER BY RAND() LIMIT 5

Link to comment
Share on other sites

I don't want it to return results from quizzes if there's an entry in quizzes_taken for the id_user and id_quiz

 

So for example if in quizzes_taken there's an entry for quiz 1 taken by user 00123466 - quiz 1 will not return in the results.

 

Does that make sense?

Link to comment
Share on other sites

try this instead

SELECT * FROM `quizzes` as q 
LEFT JOIN `quizzes_categories` as qc ON q.`category` = qc.`id_cat` 
LEFT JOIN `quizzes_taken` as qt ON q.`id` != qt.`id_quiz` AND qt.`id_user`='00123466' 
WHERE qt.`id` IS NULL 
ORDER BY RAND() LIMIT 5

 

ray

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.