views:

152

answers:

1

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);

SELECT price INTO pVal FROM products WHERE Id=iId;

SELECT price FROM products;

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

Thanks

+1  A: 

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.

aHunter
can you explain, what it is that you want to do? Cause right now I would think triggers might be what you want.
sfossen