tags:

views:

56

answers:

2

Hello! In my database, I have a "users", a "quests" and a "questings" table. A user can solve a quest. Solving a quest will save the "user_id" and the "quest_id" in my "questings" table.

Now, I want to select all quests, a user has NOT solved (meaning there is no entry for this user and quest in "questings" table)!

Let's say the user has the id 14. How to write this query?

After solving this query, I want to filter the results, too. A quest and a user has a city, too. What to do for writing a query which returns all quests, a user has NOT solved yet, in the users city (user city == quest city)?

+2  A: 
select * from quests q, users u
  where u.id = 14 AND q.city=u.city AND 
          q.id not in ( select DISTINCT(quest_id) from questing );
Salil
thank you! now it works!
Newbie
+5  A: 
SELECT *
  FROM Quests
 WHERE Quest_ID NOT IN (
       SELECT DISTINCT(Quest_ID)
         FROM Questing
        WHERE User_ID = 14)
Ardman
And where do you get User_ID from? AS I see it, there is no User_ID field in the quests table...
Maximilian Mayerl
user_id is in table 'Questing'.
Salil
@Maximilian Mayerl: Corrected SQL statement
Ardman
what about city?
Salil
thank you! now it works!
Newbie