我想知道OR
在JOIN
內部出了什麼問題,因爲它似乎沒有響應兩個條件:(k0.keyword = 'Nombre' AND k0.value LIKE 'Cliente3') OR (k0.keyword = 'Nombre' AND k0.value = 'Cliente4'))
。它總是返回完整的表格內容,並且應該只返回5組。我目前正在使用多個連接,我只是在測試一些東西,並提出這個問題,並希望瞭解這種行爲。OR inside MySQL中的連接
SELECT d.id, d.name, d.created, d.updated
FROM documents d
INNER JOIN permissions p
ON (d.wfid = p.wfId AND d.docid = p.docId AND p.userid = 1)
INNER JOIN keywords k0
ON (k0.document_id = d.id AND (k0.keyword = 'Nombre' AND k0.value LIKE 'Cliente3')
OR (k0.keyword = 'Nombre' AND k0.value = 'Cliente4'))
ORDER BY k0.value DESC, d.id DESC LIMIT 0, 12;
+------+-----------------------------+---------------------+---------------------+
| id | name | created | updated |
+------+-----------------------------+---------------------+---------------------+
| 5355 | Documento - Digitalización | 2014-03-31 12:57:51 | 2014-03-31 12:57:51 |
| 5354 | Documento - Digitalización | 2014-03-28 11:14:11 | 2014-03-28 11:14:11 |
| 5270 | DOC2 | 2014-03-07 11:41:23 | 2014-02-25 13:41:57 |
| 5269 | DOC2 | 2014-03-07 11:41:23 | 2014-02-25 12:39:51 |
| 5268 | DOC2 | 2014-03-07 11:41:23 | 2014-02-25 12:39:25 |
| 5267 | DOC1 | 2014-03-07 11:41:23 | 2014-02-25 12:39:10 |
| 5266 | DOC1 | 2014-03-07 11:41:23 | 2014-03-06 01:35:16 |
| 5265 | DOC1 | 2014-03-07 11:41:23 | 2014-02-25 12:38:37 |
| 5264 | DOC1 | 2014-03-07 11:41:23 | 2014-02-25 12:38:25 |
| 5263 | DOC1 | 2014-04-07 11:23:00 | 2014-03-20 09:54:30 |
| 5269 | DOC2 | 2014-03-07 11:41:23 | 2014-02-25 12:39:51 |
| 5268 | DOC2 | 2014-03-07 11:41:23 | 2014-02-25 12:39:25 |
+------+-----------------------------+---------------------+---------------------+
12 rows in set (0.00 sec)
mysql> select * from keywords where keyword = 'Nombre';
+-------+-------------+---------+----------+
| id | document_id | keyword | value |
+-------+-------------+---------+----------+
| 15888 | 5263 | Nombre | Cliente1 |
| 15889 | 5264 | Nombre | Cliente1 |
| 15890 | 5265 | Nombre | Cliente1 |
| 15891 | 5266 | Nombre | Cliente2 |
| 15892 | 5267 | Nombre | Cliente3 |
| 15893 | 5268 | Nombre | Cliente3 |
| 15894 | 5269 | Nombre | Cliente3 |
| 15895 | 5270 | Nombre | Cliente4 |
+-------+-------------+---------+----------+
8 rows in set (0.00 sec)
可能與你的括號有關嗎?試試這個:'ON k0.document_id = d.id AND((k0.keyword ='Nombre'AND k0.value LIKE'Cliente3')OR(k0.keyword ='Nombre'AND k0.value ='Cliente4')) '。或者因爲他們都分享'k0.keyword ='Nombre'',請嘗試:'ON k0.document_id = d.id AND k0.keyword ='Nombre'AND k0.value IN('Cliente3','Cliente4')'。 –