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
我认为问题是,子查询不返回数组,它是从声明的预期。
更新了错误消息的问题 –