我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函數 –