MySQL function to compare values ​​in db table with previous

I am new to functions in SQL, and I would like to create a function to compare values ​​in a MySQL table with previous ones, and I'm not sure how to do this.

For example (iId is an input value)

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

 SELECT price INTO pVal FROM products WHERE Id = iId;

 SELECT price FROM products;

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

thanks

+4
source share
1 answer

I was not sure how to start the select query in the table, and then return from this function, return several values ​​after they were processed. Also, I did not know whether it is possible to run a SELECT query in a function that returns more than one row. The first answer is that you cannot return an array of data or, I think, more than one line from a function. Therefore, I believe that the best way to do this is to create a temporary table with the returned new dataset.

Example

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

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

Sorry, I'm pretty new to MySQL functions.

+1
source

Source: https://habr.com/ru/post/1304205/


All Articles