2016-01-13 85 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我改變了代碼示例, –