Join
無法讓我的 JOIN 適合此查詢
使用http://sqlfiddle.com/#!18/fce3e/1上的 SQL Fiddle ,我試圖得到以下結果,但無法理解它。我不想指定特定的類別或 personId 值。
謝謝你。
| id | message | category | personId | enabled | |----|---------|----------|----------|---------| | 1 | Hello | 1 | 1 | 1 | | 2 | Hi | 1 | 1 | 1 | | 3 | Hey | 1 | 1 | 0 | | 4 | Argh | 2 | 1 | 0 | | 5 | Yar | 2 | 1 | 0 | | 6 | Hooray | 2 | 1 | 0 | | 1 | Hello | 1 | 2 | 0 | | 2 | Hi | 1 | 2 | 0 | | 3 | Hey | 1 | 2 | 0 | | 4 | Argh | 2 | 2 | 1 | | 5 | Yar | 2 | 2 | 1 | | 6 | Hooray | 2 | 2 | 0 | | 1 | Hello | 1 | 3 | 0 | | 2 | Hi | 1 | 3 | 0 | | 3 | Hey | 1 | 3 | 1 | | 4 | Argh | 2 | 3 | 0 | | 5 | Yar | 2 | 3 | 0 | | 6 | Hooray | 2 | 3 | 0 |
SELECT message.Id, message.message, message.category, person.Id personId, CASE WHEN personmessage.messageId IS NULL THEN 0 ELSE 1 END enabled FROM message CROSS JOIN person LEFT JOIN personmessage ON personmessage.personId = person.Id AND personmessage.messageId = message.Id ORDER BY 4,1