MySQL 函数将数据库表中的值与之前的值进行比较
我对 SQL 中的函数很陌生,我想创建一个函数来将 MySQL 表中的值与以前的值进行比较,但我不知道如何执行此操作。
例如(iId是输入值)
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;
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定如何在表上运行选择查询,然后在操作多个值后从该函数返回返回多个值。我还不确定是否可以在返回多行的函数中运行 SELECT 查询。第一个答案是您不能返回数据数组,或者我认为函数中不止一行。因此,我认为最好的方法是使用返回的新数据集创建一个临时表。
示例
其次,答案是肯定的,您可以在函数内运行 SELECT 查询以返回多行。
抱歉,我对 MySQL 函数还很陌生。
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
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.