简体   繁体   English

MySQL更新。 过程结果和phpmyadmin sql之间的区别

[英]MySql update. difference between procedure results and phpmyadmin sql

I am having difficult getting a procedure to update a table in the way I require. 我很难以我需要的方式获得更新表的过程。 I am using phpmyadmin on my local computer. 我在本地计算机上使用phpmyadmin。 In phpmyadmin I can put the following code into the SQL tab and one row will be updated: 在phpmyadmin中,我可以将以下代码放入SQL选项卡中,并且将更新一行:

SET `adjCost` = 22.05 WHERE `Name` LIKE CONCAT('magic', '%') AND `idKey` = '2016fulham02345';

As expected and wanted, IF the name begins with magic AND the idKey is '2016fulham02345' THEN the adjCost is updated to 22.05. 如预期和期望的那样,如果名称以魔术开头并且idKey为'2016fulham02345',那么adjCost将更新为22.05。

There will be between 2 and 50 rows with the same idKey. 将有2到50行具有相同的idKey。 The Name will never be repeated in a set with the same idKey. 名称永远不会在具有相同idKey的集合中重复。

I created a procedure with the following parameters: 我使用以下参数创建了一个过程:

IN idK VARCHAR 255 Charset    
IN aName VARCHAR 255 Charset    
IN cost FLOAT 5,2

BEGIN 
    UPDATE `raceresults` SET `adjCost` = cost WHERE `Name` LIKE CONCAT(aName, '%') AND `idKey` = idK; 
END

When I run this procedure it updates ALL adjCost where the idKey = idk and (seems) to ignore the name parameter. 当我运行此过程时,它将更新所有adjCost,其中idKey = idk和(似乎)忽略name参数。

I have tried concatenating the name string first: 我尝试过首先连接名称字符串:

BEGIN 
    SELECT CONCAT(aName, '%') INTO @str; 
    UPDATE `raceresults` SET `adjCost` = cost WHERE `Name` = @str AND `idKey` = idK; 
END

but to no avail. 但无济于事。

I looked through w3schools, stackoverflow and google and have not been able to find the answer. 我通过w3schools,stackoverflow和google进行了查看,但无法找到答案。

My question is: 我的问题是:

How can I correct my procedure to get it to work as I would like? 我该如何纠正我的程序以使其能够正常工作?

UPDATE: as requested. 更新:根据要求。

    CREATE DEFINER=`root`@`localhost` PROCEDURE `importAltUpdateAjdCost`(IN `idK` VARCHAR(255), IN `aName` VARCHAR(255), IN `cost` FLOAT(5,2))
    NO SQL
BEGIN
UPDATE `costingPP`
SET `adjCost` = cost
WHERE 
`Name` LIKE CONCAT(aName, '%')
AND
`idKey` = idK;
END

To get this, I selected export on my list of procedures on phpmyadmin. 为此,我在phpmyadmin的过程列表中选择了export。

I'm not entirely sure what or how you did, but here's what I did and it instantly worked. 我不确定您的工作方式或方式,但是我确实做了,并且立即起作用。 Since you didn't specify MySQL version, I used 5.7. 由于您未指定MySQL版本,因此我使用5.7。

EDIT: Now as I went back to see your procedure creation statement I realised that NO SQL was introduced in MySQL 8.0. 编辑:现在,当我返回查看您的过程创建语句时,我意识到MySQL 8.0中没有引入SQL Since your procedure clearly is SQL then please remove the NO SQL and re-create the procedure. 由于您的过程显然是SQL,因此请删除NO SQL并重新创建该过程。

I'm leaving my MySQL 5.7 sample here for reference: 我将MySQL 5.7示例留在此处以供参考:

1) Created a simple table: 1)创建一个简单的表:

mysql> CREATE TABLE raceresults (
->   idKey VARCHAR(255),
->   Name VARCHAR(255),
->   adjCost FLOAT(5,2)
-> );
Query OK, 0 rows affected (0.06 sec)

2) Here we insert a sample data row: 2)在此处插入示例数据行:

mysql> INSERT INTO raceresults VALUES ('2016fulham02345', 'magicFlyingHorse', 0.00);
Query OK, 1 row affected (0.01 sec)

3) To create a (STORED) PROCEDURE we have to temporarily set a different delimiter, so query parser wouldn't terminate procedure creation on default semi-colon, as it's used inside the procedure. 3)要创建一个(存储的)过程,我们必须临时设置一个不同的定界符,因此查询解析器不会终止默认分号上的过程创建,因为它是在过程内部使用的。 After delimiter's change we create the procedure and set the delimiter back to semi-colon 更改定界符后,我们创建过程并将定界符设置回分号

mysql> DELIMITER //
mysql> CREATE PROCEDURE update_test(IN idK VARCHAR(255), IN aName VARCHAR(255), IN cost FLOAT(5,2))
-> BEGIN
->     UPDATE `raceresults` SET `adjCost` = cost WHERE `Name` LIKE CONCAT(aName, '%') AND `idKey` = idK;
-> END//
mysql> DELIMITER ;
Query OK, 0 rows affected (0.00 sec)

4) Now let's see how it all works. 4)现在让我们看看它是如何工作的。 Before and after the procedure call I'm selecting the rows from database. 在过程调用之前和之后,我从数据库中选择行。 You can see the cost column value changing: 您可以看到费用列的值发生了变化:

mysql> SELECT * FROM raceresults;
+-----------------+------------------+---------+
| idKey           | Name             | adjCost |
+-----------------+------------------+---------+
| 2016fulham02345 | magicFlyingHorse |    0.00 |
+-----------------+------------------+---------+
1 row in set (0.00 sec)

mysql> CALL update_test('2016fulham02345', 'magic', 1.23);
Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM raceresults;
+-----------------+------------------+---------+
| idKey           | Name             | adjCost |
+-----------------+------------------+---------+
| 2016fulham02345 | magicFlyingHorse |    1.23 |
+-----------------+------------------+---------+
1 row in set (0.00 sec)

And now one piece of advise too: If possible, use only lower case table, column, indexes, functions, procedures, etc... names, while always writing all SQL commands in uppercase (which you did). 现在也有一条建议:如果可能,仅使用小写的表,列,索引,函数,过程等...名称,同时始终以大写形式编写所有SQL命令(您已这样做)。 This is kind of a de facto standard and makes life easier both for you and others reading your code. 这是一种事实上的标准,它使您和其他阅读代码的人的生活更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM