0

表1-测试题

| ID |       QUESTION | ANSWER | STATUS |
-----------------------------------------
|  1 | first question |  demo1 |   done |
|  2 |    second que. |  demo2 |   done |
|  3 |      Third que |  demo3 |   done |
|  4 |     Fourth que |  demo4 |   done |

表 2-Playedby

| ID | PLAY_ID | QUEST_ID | PLAYER_ANSWER |
-------------------------------------------
|  1 |       5 |        1 |             c |
|  2 |       4 |        1 |             a |
|  3 |       5 |        2 |             d |
|  4 |       4 |        2 |             b |
|  5 |       4 |        3 |             b |
|  6 |       4 |        4 |             c |
|  7 |       1 |        1 |             a |
|  8 |       3 |        1 |             c |
|  9 |       1 |        3 |             d |

我想获取那些没有被用户玩过的问题。以用户 1 为例。
我想获取用户 1 没有玩过的问题。我该怎么做?

4

1 回答 1

1

试试LEFT JOIN这样:

   SELECT q.*
     FROM testquestion q
LEFT JOIN Playedby p
       ON q.id = p.quest_id
      AND play_id = 1
    WHERE p.play_ID IS NULL

输出(对于Play_id = 1

╔════╦═════════════╦════════╦════════╗
║ ID ║  QUESTION   ║ ANSWER ║ STATUS ║
╠════╬═════════════╬════════╬════════╣
║  2 ║ second que. ║ demo2  ║ done   ║
║  4 ║ Fourth que  ║ demo4  ║ done   ║
╚════╩═════════════╩════════╩════════╝

看到这个 SQLFiddle

于 2013-05-15T09:38:12.757 回答