1

我有一张人桌:

person_id | name
1         | bob
2         | jane

他们可以回答的问题表:

qu_id | question
1     | How old are you?
2     | How tall are you?

还有一张答案表:

answer_id | qu_id | person_id | answer
10        | 1     | 2         | 24

什么查询将返回未回答的人员和问题列表:

例如,未回答的问题:

qu_id | person_id
1     | 1
2     | 1
2     | 2

(我使用的是 SQL Server Express 2012)

4

3 回答 3

4
SELECT p.person_id, q.qu_id
FROM Person p
CROSS JOIN Question q

EXCEPT

SELECT person_id, qu_id
FROM Answers
于 2013-03-08T12:26:36.367 回答
1
SELECT  b.qu_ID, a.person_ID
FROM    people a
        CROSS JOIN question b
        LEFT JOIN answer c
            ON  a.person_ID = c.person_ID AND
                b.qu_ID = c.qu_id
WHERE   c.person_ID IS NULL
ORDER   BY b.qu_id, a.person_id

结果

╔═══════╦═══════════╗
║ QU_ID ║ PERSON_ID ║
╠═══════╬═══════════╣
║     1 ║         1 ║
║     2 ║         1 ║
║     2 ║         2 ║
╚═══════╩═══════════╝
于 2013-03-08T12:25:14.037 回答
1
SELECT q.qu_id, p.person_id
FROM people AS p
CROSS JOIN questions AS q
WHERE NOT EXISTS 
(
    SELECT 1 
    FROM answers AS a 
    WHERE a.qu_id = q.qu_id 
    AND a.person_id = p.person_id
)

或者,

SELECT q.qu_id, p.person_id
FROM people AS p
CROSS JOIN questions AS q
EXCEPT
SELECT qu_id, person_id
FROM answers
于 2013-03-08T12:25:27.480 回答