MYSQL-在INSERT语句中使用函数

MYSQL-在INSERT语句中使用函数,mysql,sql-insert,last-insert-id,Mysql,Sql Insert,Last Insert Id,我正在将数据从电子表格移动到MySQL。 所以我们知道在电子表格中通常没有ID,只有文本 City;Country;... New York;USA;... Berlim;Germany;... Munich,Germany,... 考虑到这一点,让我们考虑两个表: 国家:[身份证,姓名] 城市:[身份证,国家(FK),姓名] 我不想创建几个同名的国家,但我想使用现有的一个。很好,让我们在INSERT状态下添加一个函数,用于搜索、插入(如果需要)并返回国家ID 因此,我创建了一个函数,首先评估

我正在将数据从电子表格移动到MySQL。 所以我们知道在电子表格中通常没有ID,只有文本

City;Country;...
New York;USA;...
Berlim;Germany;...
Munich,Germany,...
考虑到这一点,让我们考虑两个表:

国家:[身份证,姓名]

城市:[身份证,国家(FK),姓名]

我不想创建几个同名的国家,但我想使用现有的一个。很好,让我们在INSERT状态下添加一个函数,用于搜索、插入(如果需要)并返回国家ID

因此,我创建了一个函数,首先评估该国家是否存在,如果不存在,则创建一个国家

getCountry (parameter IN strCountry varchar(100))
然后我有成千上万的插入,比如

INSERT INTO city (name, country) VALUES ('name of the city', getCountry('new or existing one'));
该函数在单独执行时运行良好,例如

SELECT getCountry('Aruba');
然而,当我在很长的SQL(22K+行)中执行时,它就不起作用了。。。。它基本上使用开始执行之前创建的最新ID。也许我应该“等待”函数执行并返回正确的结果?但是怎么做呢


我做错了什么?

我找不到任何文档,但在另一个
插入过程中调用的函数中执行
插入时,可能会发生冲突。因此,请尝试使用变量将其拆分:

SELECT @country := getCountry('new or existing one');
INSERT INTO city (name, country) VALUES ('name of the city', @country);

使用@Barman的思想,再加上向每行添加COMMIT我可以解决以下问题:

SELECT @id := getCountry("Colombia");INSERT into city ( city, country) VALUES ('Bogota',@id);COMMIT;
SELECT @id := getCountry("Colombia");INSERT into city ( city, country) VALUES ('Medelin',@id);COMMIT;
SELECT @id := getCountry("Brazil");INSERT into city ( city, country) VALUES ('Medelin',@id);COMMIT;
SELECT @id := getCountry("Brazil");INSERT into city ( city, country) VALUES ('Sao Paulo',@id);COMMIT;
SELECT @id := getCountry("Brazil");INSERT into city ( city, country) VALUES ('Curitiba',@id);COMMIT;
SELECT @id := getCountry("USA");INSERT into city ( city, country) VALUES ('Boston',@id);COMMIT;
SELECT @id := getCountry("USA");INSERT into city ( city, country) VALUES ('DallaS',@id);COMMIT;

没有每行末尾的提交,MySQL不再计算变量,而是抛出它收集的最后一个结果。

为什么不使用存储过程来代替函数,然后该过程将处理检查和插入

如果你想执行一个程序

CALL sp_city_add('Bogota', 'Colombia');
CALL sp_city_add('Phnom Penh', 'Cambodia');
CALL sp_city_add('Yaounde', 'Cameroon');
CALL sp_city_add('Ottawa', 'Canada');
CALL sp_city_add('Santiago', 'Chile');
CALL sp_city_add('Beijing', 'China');
CALL sp_city_add('Bogotá', 'Colombia');
CALL sp_city_add('Moroni', 'Comoros');

您还可以添加一个条件来检查城市和国家是否存在,以防止重复输入。

您所说的是
值('city1',getCountry('country1'),('city2',getCountry('country2'),…
?几乎,电子表格只允许每行创建一个插入城市值('city1',getcountry1');插入城市值('city2',getCountry('country1');插入城市值('city3',getCountry('country1');嗨,巴尔马。。在INSERT语句“COMPLIES”中使用该函数,尽管它没有为我提供所需的正确ID。我应该为script.sql中的每一行执行该操作吗?有没有更好的办法?['sql']
选择@country:=getCountry('Germany');在城市(名称、国家)值中插入(‘柏林’、@country);选择@country:=getCountry(“德国”);在城市(名称、国家)值中插入(‘慕尼黑’、@country);选择@country:=getCountry(“德国”);在城市(名称、国家)值中插入('Passau',@country);选择@country:=getCountry(“巴西”);在城市(名称、国家)值中插入(‘圣保罗’、@country)['sql']是的,每个插入都需要分成两部分。虽然如果按国家分组,您可以只执行一次
getCountry()
。谢谢,@Barmar让我们尝试一下,幸好它没有正常工作,@Barmar:看看我在哪里共享脚本以及我得到的结果
DELIMITER $$
CREATE PROCEDURE `sp_city_add`(in p_city varchar(100), in p_country varchar(100))
BEGIN

DECLARE country_id INT;

IF (SELECT COUNT(1) FROM country WHERE country.country = p_country) = 0 THEN
    INSERT INTO country (country) VALUE (p_country);

    SET country_id = LAST_INSERT_ID();
ELSE
    SELECT ID INTO country_id FROM country WHERE country.country = p_country;
END IF;

INSERT INTO city (name, country) VALUES (p_city, country_id);

END$$
DELIMITER ;
CALL sp_city_add('Bogota', 'Colombia');
CALL sp_city_add('Phnom Penh', 'Cambodia');
CALL sp_city_add('Yaounde', 'Cameroon');
CALL sp_city_add('Ottawa', 'Canada');
CALL sp_city_add('Santiago', 'Chile');
CALL sp_city_add('Beijing', 'China');
CALL sp_city_add('Bogotá', 'Colombia');
CALL sp_city_add('Moroni', 'Comoros');