I am by no means an "SQL-expert", but I'll give this a shot... :)

> -----Original Message-----
> From: Richard Brenner [mailto:[EMAIL PROTECTED]]
> Subject: query problem
>
> I have two tables with the following structure:
>
> Users:
> | Field        | Type
> | id           | int(10) unsigned
> | name         | blob
[etc]
> +--------------+------------------
> answers:
> +----------+---------------------+
> | id       | int(10) unsigned    |
> | userid   | int(10) unsigned    |
> | question | int(10) unsigned    |
> | correct  | set('true','false') |
> | date     | date                |
> +----------+---------------------+
> This is for a quiz. Every user has to answer 4 questions ...
> I want to print out all users, that
> have answered all 4 questions correct.
> Can I do this with one query?

I built a couple of tables similar to what you have [trimmed to the
"essentials"], populated with some data, and ran the following:

SELECT   id,name,userid,correct,count(correct) as numcorrect
FROM     users,answers
WHERE    id=userid AND correct='true'
GROUP BY id

which created:
+----+---------+--------+---------+------------+
| id | name    | userid | correct | numcorrect |
+----+---------+--------+---------+------------+
|  1 | alfred  |      1 | true    |          3 |
|  2 | bobby   |      2 | true    |          4 |
|  3 | carol   |      3 | true    |          2 |
|  4 | diane   |      4 | true    |          3 |
|  5 | edward  |      5 | true    |          3 |
|  6 | frank   |      6 | true    |          3 |
|  7 | george  |      7 | true    |          1 |
|  8 | harry   |      8 | true    |          3 |
|  9 | larry   |      9 | true    |          1 |
| 10 | mark    |     10 | true    |          3 |
| 11 | nancy   |     11 | true    |          4 |
| 12 | oliver  |     12 | true    |          3 |
| 13 | paul    |     13 | true    |          2 |
| 14 | quentin |     14 | true    |          4 |
| 15 | ralph   |     15 | true    |          3 |
| 16 | samuel  |     16 | true    |          3 |
| 17 | thomas  |     17 | true    |          1 |
| 18 | ursula  |     18 | true    |          4 |
| 19 | victor  |     19 | true    |          2 |
| 20 | walter  |     20 | true    |          3 |
| 21 | xavier  |     21 | true    |          4 |
| 22 | yvonne  |     22 | true    |          3 |
| 23 | zack    |     23 | true    |          2 |
| 24 | igor    |     24 | true    |          3 |
| 25 | jack    |     25 | true    |          4 |
| 26 | kristen |     26 | true    |          4 |
+----+---------+--------+---------+------------+

unfortunately, adding "and numcorrect=4" to the WHERE clause caused an error
(numcorrect undefined), but if this were put into a temporary table, it is
then trivial to

   SELECT * FROM results WHERE numcorrect=4;


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to