如何更新和插入 MySQL 中的特定条件?
how to update and insert on specific condition in MySQL?
我有一个table名字'test':
create table test(main_mix_number varchar(20),formulation_mix_number varchar(20),param1 varchar(20),param2 varchar(20));
它有以下行:
insert into test(formulation_mix_number,param1,param2) values ('101A','abc','xyz');
+-----------------+------------------------+--------+--------+
| main_mix_number | formulation_mix_number | param1 | param2 |
+-----------------+------------------------+--------+--------+
| NULL | 101A | abc | xyz |
+-----------------+------------------------+--------+--------+
现在,
if number of row where 'main_mix_number' is 'NULL' equals 1 then I want to update the row
else if number of rows where 'main_mix_number' is 'NULL' greater than 1 then I want to update the first row
else if number of rows where 'main_mix_number' is 'NULL' equals 0 then row insertion took place.
所以,我尝试了以下查询:
select (
case when (select count(distinct(formulation_mix_number)) from test where formulation_mix_number='101A' and main_mix_number is NULL)=1 then
(update test set main_mix_number='1A' where formulation_mix_number='101A')
when (select count(distinct(formulation_mix_number)) from test where formulation_mix_number='101A' and main_mix_number is NULL)>1 then
(update test set main_mix_number='1A' where formulation_mix_number='101A' order by formulation_mix_number desc limit 1)
when (select count(distinct(formulation_mix_number)) from test where formulation_mix_number='101A' and main_mix_number is NULL)=0 then
(insert into test (main_mix_number,formulation_mix_number,param1,param2)
select * from (select '1A' as main_mix_number,'101A' as fromulation_mix_number,param1,param2 from test where formulation_mix_number='101A' order by fromulation_mix_number desc limit 1) as tmp
where not exists (select main_mix_number,formulation_mix_number from test where main_mix_number='1A' and formulation_mix_number='101A')) else NULL end)
from temp;
但是由于 select 和更新或插入不能一起工作,所以这个查询不起作用。我知道还有另一种方法可以将 formulation_mix_number 和 main_mix_number 保存在单独的 table 中,并且不要将 main_mix_number 与 formulation_mix_number 细节混在一起。
但是由于某些原因,我不允许在我的数据库中添加另一个 table。
这需要动态 sql
create table test(main_mix_number varchar(20),formulation_mix_number varchar(20),param1 varchar(20),param2 varchar(20));
insert into test(formulation_mix_number,param1,param2) values ('101A','abc','xyz');
SELECT
(CASE
WHEN
(SELECT
COUNT(DISTINCT (formulation_mix_number))
FROM
test
WHERE
formulation_mix_number = '101A'
AND main_mix_number IS NULL) = 1
THEN
(@sql:='update test set main_mix_number=\'1A\' where formulation_mix_number=\'101A\'')
WHEN
(SELECT
COUNT(DISTINCT (formulation_mix_number))
FROM
test
WHERE
formulation_mix_number = '101A'
AND main_mix_number IS NULL) > 1
THEN
(@sql:='update test set main_mix_number=\'1A\' where formulation_mix_number=\'101A\' order by formulation_mix_number desc limit 1')
WHEN
(SELECT
COUNT(DISTINCT (formulation_mix_number))
FROM
test
WHERE
formulation_mix_number = '101A'
AND main_mix_number IS NULL) = 0
THEN
(@sql:='insert into test (main_mix_number,formulation_mix_number,param1,param2)
select * from (select \'1A\' as main_mix_number,\'101A\' as fromulation_mix_number,param1,param2 from test where formulation_mix_number=\'101A\' order by fromulation_mix_number desc limit 1) as tmp
where not exists (select main_mix_number,formulation_mix_number from test where main_mix_number=\'1A\' and formulation_mix_number=\'101A\')')
ELSE NULL
END)
FROM
test
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE stmt;
SELECT * FROM test
main_mix_number | formulation_mix_number | param1 | param2
:-------------- | :--------------------- | :----- | :-----
1A | 101A | abc | xyz
db<>fiddle here
我有一个table名字'test':
create table test(main_mix_number varchar(20),formulation_mix_number varchar(20),param1 varchar(20),param2 varchar(20));
它有以下行:
insert into test(formulation_mix_number,param1,param2) values ('101A','abc','xyz');
+-----------------+------------------------+--------+--------+
| main_mix_number | formulation_mix_number | param1 | param2 |
+-----------------+------------------------+--------+--------+
| NULL | 101A | abc | xyz |
+-----------------+------------------------+--------+--------+
现在,
if number of row where 'main_mix_number' is 'NULL' equals 1 then I want to update the row
else if number of rows where 'main_mix_number' is 'NULL' greater than 1 then I want to update the first row
else if number of rows where 'main_mix_number' is 'NULL' equals 0 then row insertion took place.
所以,我尝试了以下查询:
select (
case when (select count(distinct(formulation_mix_number)) from test where formulation_mix_number='101A' and main_mix_number is NULL)=1 then
(update test set main_mix_number='1A' where formulation_mix_number='101A')
when (select count(distinct(formulation_mix_number)) from test where formulation_mix_number='101A' and main_mix_number is NULL)>1 then
(update test set main_mix_number='1A' where formulation_mix_number='101A' order by formulation_mix_number desc limit 1)
when (select count(distinct(formulation_mix_number)) from test where formulation_mix_number='101A' and main_mix_number is NULL)=0 then
(insert into test (main_mix_number,formulation_mix_number,param1,param2)
select * from (select '1A' as main_mix_number,'101A' as fromulation_mix_number,param1,param2 from test where formulation_mix_number='101A' order by fromulation_mix_number desc limit 1) as tmp
where not exists (select main_mix_number,formulation_mix_number from test where main_mix_number='1A' and formulation_mix_number='101A')) else NULL end)
from temp;
但是由于 select 和更新或插入不能一起工作,所以这个查询不起作用。我知道还有另一种方法可以将 formulation_mix_number 和 main_mix_number 保存在单独的 table 中,并且不要将 main_mix_number 与 formulation_mix_number 细节混在一起。 但是由于某些原因,我不允许在我的数据库中添加另一个 table。
这需要动态 sql
create table test(main_mix_number varchar(20),formulation_mix_number varchar(20),param1 varchar(20),param2 varchar(20));
insert into test(formulation_mix_number,param1,param2) values ('101A','abc','xyz');
SELECT (CASE WHEN (SELECT COUNT(DISTINCT (formulation_mix_number)) FROM test WHERE formulation_mix_number = '101A' AND main_mix_number IS NULL) = 1 THEN (@sql:='update test set main_mix_number=\'1A\' where formulation_mix_number=\'101A\'') WHEN (SELECT COUNT(DISTINCT (formulation_mix_number)) FROM test WHERE formulation_mix_number = '101A' AND main_mix_number IS NULL) > 1 THEN (@sql:='update test set main_mix_number=\'1A\' where formulation_mix_number=\'101A\' order by formulation_mix_number desc limit 1') WHEN (SELECT COUNT(DISTINCT (formulation_mix_number)) FROM test WHERE formulation_mix_number = '101A' AND main_mix_number IS NULL) = 0 THEN (@sql:='insert into test (main_mix_number,formulation_mix_number,param1,param2) select * from (select \'1A\' as main_mix_number,\'101A\' as fromulation_mix_number,param1,param2 from test where formulation_mix_number=\'101A\' order by fromulation_mix_number desc limit 1) as tmp where not exists (select main_mix_number,formulation_mix_number from test where main_mix_number=\'1A\' and formulation_mix_number=\'101A\')') ELSE NULL END) FROM test
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE stmt;
SELECT * FROM test
main_mix_number | formulation_mix_number | param1 | param2 :-------------- | :--------------------- | :----- | :----- 1A | 101A | abc | xyz
db<>fiddle here