将数据添加到mysql数据库中的表之前请检查
我试图在表emp_master中插入数据,其中令牌号和车辆号可用,我使用下面提到的查询来添加和检查记录将数据添加到mysql数据库中的表之前请检查,mysql,sql,mysql-workbench,mysql-python,mysql-error-1064,Mysql,Sql,Mysql Workbench,Mysql Python,Mysql Error 1064,我试图在表emp_master中插入数据,其中令牌号和车辆号可用,我使用下面提到的查询来添加和检查记录 insert into emp_master (token_no, vehicle_no) values (1234,12345) where not exists (select * from emp_master where vehicle_no = '12345'); 但每当我尝试这个,错误就会出现 ERROR 1064 (42000): You have an error in yo
insert into emp_master (token_no, vehicle_no) values (1234,12345) where not exists (select * from emp_master where vehicle_no = '12345');
但每当我尝试这个,错误就会出现
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where not exists (select * from emp_master where vehicle_no = '12345')' at line 1
该建议将很有帮助在vehicle_no(车辆编号)列上添加一个,然后插入不带WHERE(位置)子句的数据。相反,如果异常失败,请处理它。您可以使用查询来实现此功能:
INSERT INTO emp_master (token_no, vehicle_no)
SELECT 1234, 12345
WHERE NOT EXISTS (SELECT * FROM emp_master WHERE vehicle_no = '12345')
如果需要在insert语句中进行此检查,可以按如下方式构造它: SQL Fiddle: 在任何DBMS上执行此操作的另一种方法:
INSERT语句中不能有WHERE。您需要在插入触发器之前创建并检查那里的所有内容。或者使用FK阻止插入与不存在的标识符相关的记录。此链接可能有助于解决您的问题吗?如果没有,你能提供更多的信息来帮助回答这个问题吗?否则,请考虑把答案最好的答案标在上下投票箭头上。看见
insert into emp_master (token_no, vehicle_no)
select 1234, 12345
from dual
where not exists (select * from emp_master where vehicle_no = '12345');
insert into emp_master (token_no, vehicle_no)
select token_no, vehicle_no
from (
select 1234 token_no, 12345 vehicle_no
) rec
where not exists (select * from emp_master where vehicle_no = rec.vehicle_no);