0

私は現在(かなり簡単だと思う)SQLの問題に苦しんでいますが、それを理解できないようです。

次のテーブルがあるとします。

Persons
+-------+----+
| name  | id | 
+-------+----+
| Steve | 1  | 
| John  | 2  | 
+-------+----+

Information
+----------+----+-----------+---------------------------------------------------+
| type     | id | linked_id | info                                              |
+----------+----+-----------+---------------------------------------------------+
| persons  | 1  | 1         | Info about Steve                                  |
| cars     | 2  | 1         | Info about a car, aka not stored in Persons table |
+----------+----+-----------+---------------------------------------------------+

Persons テーブルと Information のサブセット (type=persons) が必要な場合、クエリは次のようになります。

SELECT * 
FROM Persons
LEFT JOIN Information ON Persons.id = Information.linked_id
WHERE (Information.type = "persons" OR Information.type IS NULL)

そして、これは私が期待するものでなければなりません:

Desired Result
+-------+----+----------+------+------------+------------------+
| name  | id | type     | id   | linked_id  | info             |
+-------+----+----------+------+------------+------------------+
| Steve | 1  | persons  | 1    | 1          | Info about Steve |
| John  | 2  | NULL     | NULL | NULL       | NULL             |
+-------+----+----------+------+------------+------------------+

しかし、これは実際の結果です:

+-------+----+----------+----+-----------+------------------+
| name  | id | type     | id | linked_id | info             |
+-------+----+----------+----+-----------+------------------+
| Steve | 1  | persons  | 1  | 1         | Info about Steve |
+-------+----+----------+----+-----------+------------------+

Information 行をまだ持っていない "John" Person 行も結果に含まれる必要がありますが、含まれていません。

私は何を間違っていますか?OR Information.type IS NULLクエリの一部でこれを処理する必要はありませんか? ただし、行は含まれていません。他に何か不足していますか?

4

1 に答える 1