mysql> SELECT * FROM swaps_products;
+----+---------+-----------+------------+-----------+---------------------+
| id | swap_id | holder_id | swap_to_id | product_id| added |
+----+---------+-----------+------------+-----------+---------------------+
| 1 | 9 | 3 | 0 | 32 | 2017-01-06 10:43:52 |
| 2 | 11 | 13 | 0 | 3 | 2017-01-06 11:03:45 |
| 3 | 11 | 13 | 0 | 4 | 2017-01-06 11:03:45 |
| 4 | 11 | 3 | 0 | 32 | 2017-01-06 11:03:45 |
| 5 | 11 | 3 | 0 | 31 | 2017-01-06 11:03:45 |
| 6 | 11 | 3 | 0 | 30 | 2017-01-06 11:03:45 |
| 7 | 12 | 3 | 0 | 32 | 2017-01-06 14:16:13 |
| 8 | 12 | 3 | 0 | 31 | 2017-01-06 14:16:13 |
| 9 | 12 | 2 | 0 | 2 | 2017-01-06 14:16:13 |
| 10 | 12 | 2 | 0 | 1 | 2017-01-06 14:16:13 |
| 11 | 13 | 13 | 3 | 3 | 2017-01-12 14:31:44 |
| 12 | 13 | 13 | 3 | 4 | 2017-01-12 14:31:44 |
| 13 | 13 | 3 | 13 | 32 | 2017-01-12 14:31:44 |
| 14 | 13 | 3 | 13 | 31 | 2017-01-12 14:31:44 |
要swap_id = 13
有涉及四款產品 - 3,4,31,32。我需要一個查詢,它會給我所有其他交換(swap_id的列表),其中有3,4,31,32個產品中的任何一個交換了。
我有這個
SELECT sp.swap_id as swap_id, sp.holder_id as holder_id
-> FROM swaps_products as sp
-> JOIN swaps as s
-> ON s.id=sp.swap_id
-> WHERE (sp.swap_id != 13) AND
-> (s.rejected IS NULL) AND
-> ((s.swapped IS NULL) OR (s.swapped2 IS NULL)) AND
-> (sp.product_id IN (3,4,31,32));
連接是沒有問題的重要。這個查詢的工作原理,但問題是,我只知道swap_id。所以,我想這一點:
(sp.product_id IN (SELECT product_id FROM swaps_products WHERE swap_id = 13));
,但得到的錯誤:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN swaps as s
ON s.id=sp.swap_id
WHERE (sp.swap_id != 13) AND
(s.rejected IS N' at line 2
我認爲問題是,子查詢不返回數組,它是從聲明的預期。
更新了錯誤消息的問題 –