开发者

MySQL function to compare values in a db table against the previous

开发者 https://www.devze.com 2022-12-22 20:22 出处:网络
Iam quite new to functions in SQL and I would like to create a function to compare values in a MySQL table against previous and I am not sure how to do this.

Iam quite new to functions in SQL and I would like to create a function to compare values in a MySQL table against previous and I am not sure how to do this.

For example (iId is the input value)

DECLARE pVal INT(20);
DECLARE val INT(20);

SEL开发者_Go百科ECT price INTO pVal FROM products WHERE Id=iId;

SELECT price FROM products;

IF price == pVal THEN
SET val = price;
END IF;

Thanks


I was not sure how to run a select query on a table and then return from that function return multiple values once they have been manipulated. Also I was unsure if you could run a SELECT query in a function that returns more than one row. The first answer is that you can not return an array of data or I think more than one row from a function. Therefore I think the best way to do this is to create a temporary table with the new dataset returned.

Example


DROP TEMPORARY TABLE IF EXISTS employeeTemp;
   CREATE TEMPORARY TABLE employeeTemp AS
   SELECT id,start_date
    FROM employee;

Secondly the answer is yes you can run a SELECT query inside a function to return more than one row.

Sorry about this I am quite new to MySQL functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消