我followed this tutorial,并写了这个查询:使用IN比较MySql中与GROUP_CONCAT
SELECT * FROM table1 WHERE ID IN (SELECT ID IN table2);
输出:它只是只选择第一行,但这里作为
SELECT * FROM table1 WHERE ID IN (1,2);
输出:选择两行
这些都是我使用
mysql> show create table tmp_product_ids;
+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| tmp_product_ids | CREATE TABLE `tmp_product_ids` (
`product__id` int(11) DEFAULT NULL,
`order__id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from tmp_product_ids;
+-------------+-----------+
| product__id | order__id |
+-------------+-----------+
| 1 | 2 |
| 2 | 1 |
+-------------+-----------+
2 rows in set (0.01 sec)
mysql> select * from tmp_product_ids where product__id in (1,2);
+-------------+-----------+
| product__id | order__id |
+-------------+-----------+
| 1 | 2 |
| 2 | 1 |
+-------------+-----------+
2 rows in set (0.00 sec)
mysql> select group_concat(product__id) from tmp_product_ids;
+---------------------------+
| group_concat(product__id) |
+---------------------------+
| 1,2 |
+---------------------------+
1 row in set (0.00 sec)
mysql> select * from tmp_product_ids where product__id in (select group_concat(product__id) from tmp_product_ids);
+-------------+-----------+
| product__id | order__id |
+-------------+-----------+
| 1 | 2 |
+-------------+-----------+
1 row in set (0.00 sec)
任何一个可以解释这种现象的精确查询? 和我怎么能写入MySQL查询删除行delete from table1 where ID in (select ID from table2)
而不使用连接? 注意: 我已经使用group_concat函数从不同表中获取ID:
为什么不使用连接?联结绝对是处理这种情况的最佳方式。 –
另请参见:我无法重现您的第一个结果。 'select * from table1'的输出,其中ID为(在table2中选择ID);'应该给出table1中ID存在于table2中的所有记录。 –
@JoelCoehoorn我不能使用连接,因为这两个表没有以任何方式连接,我想删除table1中的行,使用我从table2使用group_concat函数 –