請看下面的測試案例:
CREATE TABLE accounts (id int);
CREATE TABLE emails (id int, account_id int, type_name varchar(10), order_id int);
INSERT INTO accounts VALUES (1), (2), (3), (4);
INSERT INTO emails VALUES (1, 1, 'name', 1);
INSERT INTO emails VALUES (2, 1, 'no-name', 1);
INSERT INTO emails VALUES (3, 2, 'name', 1);
INSERT INTO emails VALUES (4, 2, 'no-name', 1);
INSERT INTO emails VALUES (5, 3, 'name', 2);
那麼這個按預期工作:
SELECT * FROM `accounts`
LEFT OUTER JOIN `emails` ON emails.account_id = accounts.id
WHERE (emails.type_name = 'name' AND emails.order_id = 1);
+------+------+------------+-----------+----------+
| id | id | account_id | type_name | order_id |
+------+------+------------+-----------+----------+
| 1 | 1 | 1 | name | 1 |
| 2 | 3 | 2 | name | 1 |
+------+------+------------+-----------+----------+
2 rows in set (0.00 sec)
與你的第二個查詢的問題是,它可以返回一個NULL
行,如果有與帳戶沒有電子郵件,爲的就是賬號4的情況下:
SELECT * FROM `accounts`
LEFT OUTER JOIN `emails` ON emails.account_id = accounts.id
WHERE (!(emails.type_name = 'name' AND emails.order_id = 1) OR emails.id IS NULL);
+------+------+------------+-----------+----------+
| id | id | account_id | type_name | order_id |
+------+------+------------+-----------+----------+
| 1 | 2 | 1 | no-name | 1 |
| 2 | 4 | 2 | no-name | 1 |
| 3 | 5 | 3 | name | 2 |
| 4 | NULL | NULL | NULL | NULL |
+------+------+------------+-----------+----------+
4 rows in set (0.01 sec)
爲什麼不這是足夠的,沒有NULL
行?:
SELECT * FROM `accounts`
LEFT OUTER JOIN `emails` ON emails.account_id = accounts.id
WHERE NOT (emails.type_name = 'name' AND emails.order_id = 1)
+------+------+------------+-----------+----------+
| id | id | account_id | type_name | order_id |
+------+------+------------+-----------+----------+
| 1 | 2 | 1 | no-name | 1 |
| 2 | 4 | 2 | no-name | 1 |
| 3 | 5 | 3 | name | 2 |
+------+------+------------+-----------+----------+
3 rows in set (0.00 sec)
感謝詳細的回覆!對於第二個查詢,我正在查找沒有特定類型電子郵件記錄的帳戶(type_name ='name'AND order_id = 1)。在你的例子中,帳戶1和2有這種電子郵件記錄,所以我不希望那些返回。帳戶3的電子郵件記錄不匹配,所以我希望返回。帳戶4沒有電子郵件記錄,所以我希望返回,因爲它不匹配。 總結:我的查詢目前返回帳戶1,2,3和4.我只希望它返回3和4. – tassock 2010-07-23 18:06:52