如何检查一个 table 中的记录并插入另一个 table 中?
How to check for a record in one table and insert in another table?
create procedure checkin_customerid(in customer_id int)
if (select c.customerID from customer_detail c
where customer_id = c.customerID)
then
insert into customer_checkin (customerID)
values (customer_id);
我正在尝试在 MySQL 中创建此过程,应该:
- 检查 table customer_details 和
中的 customerID 是否匹配
- 如果为真,应在table customer_checkin中添加customer_id。
但是我在最后一行收到一个错误:values (customer_id);其中说:
错误代码:1064。您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 4 行
附近使用的正确语法
将鼠标悬停在代码错误上说:语句不完整,需要“;”
(customer_id)
最后一行的括号带有下划线。
我该如何解决这个问题?
试试这个。您缺少 END IF;
我会将 if 子句重写为 EXISTS
CREATE PROCEDURE checkin_customerid(in customer_id int)
BEGIN
if (EXISTS(select c.customerID from customer_detail c
where customer_id = c.customerID))
then
insert into customer_checkin (customerID)
values (customer_id);
END IF;
END
但 Oiliver 仅在插入 if 子句时是正确的
create procedure checkin_customerid(in customer_id int)
if (select c.customerID from customer_detail c
where customer_id = c.customerID)
then
insert into customer_checkin (customerID)
values (customer_id);
我正在尝试在 MySQL 中创建此过程,应该:
- 检查 table customer_details 和 中的 customerID 是否匹配
- 如果为真,应在table customer_checkin中添加customer_id。
但是我在最后一行收到一个错误:values (customer_id);其中说:
错误代码:1064。您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 4 行
附近使用的正确语法将鼠标悬停在代码错误上说:语句不完整,需要“;”
(customer_id)
最后一行的括号带有下划线。
我该如何解决这个问题?
试试这个。您缺少 END IF;
我会将 if 子句重写为 EXISTS
CREATE PROCEDURE checkin_customerid(in customer_id int)
BEGIN
if (EXISTS(select c.customerID from customer_detail c
where customer_id = c.customerID))
then
insert into customer_checkin (customerID)
values (customer_id);
END IF;
END
但 Oiliver 仅在插入 if 子句时是正确的