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
https://forums.phpfreaks.com/topic/105762-solved-help-with-a-query/
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

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.