我使用此表存储一些键,并且我想更新列“value”,其中列“name”仅在用户的国家/地区为CA时才等于name_1
| 用户|名称|值|
| --|--|--|
| 1 |名称_1| 0 |
| 1 |国家|FR|
| 2 |名称_1| 0 |
| 2 |国家|CA|
| 3 |名称_1| 0 |
| 3 |国家|CA|
预期产出:
| 用户|名称|值|
| --|--|--|
| 1 |名称_1| 0 |
| 1 |国家|FR|
| 2 |名称_1|第一章|
| 2 |国家|CA|
| 3 |名称_1|第一章|
| 3 |国家|CA|
我想我可以使用存储过程来完成这个任务,但我不知道是否有更简单的方法来使用单个查询。谢谢
这是我创建的存储过程,它可以工作,但我真的很想执行一些更简单的东西。
DELIMITER //
CREATE PROCEDURE updateName1()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE userId INT;
-- Declare a cursor for the SELECT query
DECLARE cur CURSOR FOR
SELECT distinct user FROM my_table WHERE name = 'country' AND value = 'CA';
-- Declare handler for when no more rows found
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- Open the cursor
OPEN cur;
-- Loop through the result set
read_loop: LOOP
FETCH cur INTO userId;
-- Exit loop when no more rows
IF done THEN
LEAVE read_loop;
END IF;
-- Perform actions with each row here
-- For example, print the values
UPDATE my_table SET value = 1 WHERE name = 'name_1' AND user = userId;
END LOOP;
-- Close the cursor
CLOSE cur;
END //
DELIMITER ;
字符串
1条答案
按热度按时间o75abkj41#
应该是这样的:
字符串