2017-07-18 240 views
0

我試圖插入表格從結果從表B中收集空的結果:INSERT INTO與SELECT

INSERT INTO A (x, y, created_at) 
(SELECT x, "something", a_timestamp 
FROM B WHERE c IS NULL AND a_timestamp > NOW()) 

的問題是,對於某些情況下

(SELECT x, "something", a_timestamp 
FROM B WHERE c IS NULL AND a_timestamp > NOW()) 

沒有按不會返回任何記錄(這是可以的),但是另一個INSERT失敗。

我該如何掩蓋自己的情況?

謝謝!

+0

您可以事先做選擇並且對返回的行數進行計數嗎?如果它大於0,則執行上述操作。 – jacksonecac

+0

我可以,但那不會那麼高效。如果有多於0條記錄,我會再次進行選擇插入。 – content01

+0

看到我的修改回答 – jacksonecac

回答

-1
"INSERT INTO A (x, y, created_at) 
(SELECT x, \"something\", a_timestamp 
FROM B WHERE c IS NULL AND a_timestamp > \"#{current_time}\") 

"INSERT INTO A (x, y, created_at) 
(SELECT x, \"something\", a_timestamp 
FROM B WHERE c IS NULL AND a_timestamp > \"#{current_time}\") 
AND (SELECT count(x) FROM B WHERE c IS NULL AND a_timestamp > \"#{current_time}\")) > 0" 
+0

如果SELECT語句不返回任何記錄,合併將無濟於事。此外,我得到的印象是OP不希望在SELECT語句產生0條記錄時產生一條記錄。 OP僅僅希望在這種情況下不會收到錯誤。 – JNevill

+0

正確,我不想插入任何東西,只是想讓它「默默地」失敗。 @JNevill – content01

0

我無法重現錯誤:

mysql> DROP TABLE IF EXISTS `tbl_test`; 
Query OK, 0 rows affected, 1 warning (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `tbl_test` (
    -> `id` TINYINT 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> INSERT INTO `tbl_test` 
    -> SELECT 1 
    -> FROM DUAL 
    -> WHERE 0 = 0; 
Query OK, 1 row affected (0.00 sec) 
Records: 1 Duplicates: 0 Warnings: 0 

mysql> SELECT `id` FROM `tbl_test`; 
+------+ 
| id | 
+------+ 
| 1 | 
+------+ 
1 row in set (0.00 sec) 

mysql> INSERT INTO `tbl_test` 
    -> SELECT 1 
    -> FROM DUAL 
    -> WHERE 0 = 1; 
Query OK, 0 rows affected (0.00 sec) 
Records: 0 Duplicates: 0 Warnings: 0 

mysql> SELECT `id` FROM `tbl_test`; 
+------+ 
| id | 
+------+ 
| 1 | 
+------+ 
1 row in set (0.00 sec) 

UPDATE

mysql> DROP TABLE IF EXISTS `tbl_test`; 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `tbl_test` (
    -> `id` TINYINT 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> INSERT INTO `tbl_test` 
    -> (`id`) 
    -> VALUES 
    -> (101); 
Query OK, 1 row affected (0.00 sec) 

mysql> SELECT `id` 
    -> FROM `tbl_test`; 
+------+ 
| id | 
+------+ 
| 101 | 
+------+ 
1 row in set (0.00 sec) 

mysql> INSERT INTO `tbl_test` 
    -> SELECT `id` + 1 
    -> FROM `tbl_test` 
    -> WHERE `id` > 100; 
Query OK, 1 row affected (0.00 sec) 
Records: 1 Duplicates: 0 Warnings: 0 

mysql> SELECT `id` 
    -> FROM `tbl_test`; 
+------+ 
| id | 
+------+ 
| 101 | 
| 102 | 
+------+ 
2 rows in set (0.00 sec) 

mysql> INSERT INTO `tbl_test` 
    -> SELECT `id` + 1 
    -> FROM `tbl_test` 
    -> WHERE `id` < 100; 
Query OK, 0 rows affected (0.00 sec) 
Records: 0 Duplicates: 0 Warnings: 0 

mysql> SELECT `id` 
    -> FROM `tbl_test`; 
+------+ 
| id | 
+------+ 
| 101 | 
| 102 | 
+------+ 
2 rows in set (0.00 sec) 

db-fiddle

+0

您的查詢不是我的問題的反映,請在執行SELECT時使用WHERE子句中的實際數據條件(如WHERE id> 100)。 – content01

+0

@ content01:發佈測試數據以方便工作。 – wchiquito

+0

@ content01:使用與您指示的條件接近的條件更新答案。 – wchiquito