I am trying to create an SQL query wherein I need to check to sum of columns from 2 tables and then insert a new row into another table if the condition is satisfied. I know I can use a stored procedure with explicit locking but want to know is it is possible to do it in a single SQL query. I wrote the following but it gives syntax error in MySQL workbench
INSERT INTO table1 (`col1`, `col2`)
VALUES ('abc', 'xyz') WHERE
(
((select
COUNT(*) from table2 t2 where t2.id = 1)
+ (select sum(t3.counter) from table3 t3
WHERE t3.id = 1)) < (select t4.total_allowed
FROM table4 t4 where t4.id = 1)
);
This can be done because you can insert the results of a select query .
INSERT INTO table1 (`col1`, `col2`)
SELECT 'abc', 'xyz'
FROM DUAL WHERE
(
((select
COUNT(*) from table2 t2 where t2.id = 1)
+ (select sum(t3.counter) from table3 t3
WHERE t3.id = 1)) < (select t4.total_allowed
FROM table4 t4 where t4.id = 1)
);
MySQL INSERT
语法不支持WHERE
子句。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.