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 に答える