2016-01-13 94 views
1

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:

+0

为什么不使用连接?联结绝对是处理这种情况的最佳方式。 –

+0

另请参见:我无法重现您的第一个结果。 'select * from table1'的输出,其中ID为(在table2中选择ID);'应该给出table1中ID存在于table2中的所有记录。 –

+0

@JoelCoehoorn我不能使用连接,因为这两个表没有以任何方式连接,我想删除table1中的行,使用我从table2使用group_concat函数 –

回答

0

所以这里是一个快速解释。

所以你说DELETE FROM Table_1 WHERE ID IN (SubQuery);

一切将在subquery被选中,并具有相同的IDIDTable_1将被删除。

在删除之前,您可以单独执行subquery。并且您会知道选择了多少行以及有多少行与Table_1匹配。

您还可以将WHERE条件放入您的subquery

DELETE FROM Table_1 WHERE ID IN (SELECT ID From Table_2 WHERE Name = 'John');

,如果你正在写IN条款那么像这样strings

WHERE ID IN ('1','2','3');

OR

在的情况下,INT

WHERE ID IN (1,2,3);

0

您IN语句有点关闭。既然你写的是这样的:

WHERE ... IN ('2,1') 

该查询正在寻找字符串['2,1']。你需要附上每个刻度:

WHERE ... IN ('2','1') 

或者,如果这是一个整场只:

WHERE ... IN (2,1) 

希望这有助于。

+0

选择'table2中的选择ID'的结果是什么? – bdn02

+0

@Wes Palmer我已经更改了他已经提到的代码示例 –

0

来自前两个查询的结果取决于表中的数据。你最后的两个查询只返回一个布尔值,你问

是{1,1}的一个组成部分?

结果是1(真)。如果你问

是1的一个组件{'1,2'}?

它正确地返回0(假)。

您得到的警告可能是因为您正在比较整数值与字符串。另见韦斯的回答。

0

我怎么能写MySQL查询删除行

您可以编写

DELETE table1 
FROM table1 
INNER JOIN table2 ON table1.id = table2.id; 

一个简单的连接查询没有JOIN你可以和你的子查询去

delete from table1 
where ID in (select distinct ID from table2); 

您还可以使用WHERE EXISTS

delete from table1 t1 
where exists (select 1 from table2 where id = t1.id); 

同样,你的第一个查询select * from table1 where ID in (select ID in table2);返回1行的原因很可能table2只有1个记录。

做一个select count(*) from table2验证相同。

+0

,但未使用连接。 –

+1

如果有帮助,请参阅编辑。 – Rahul

+0

@rahul我改变了代码示例, –