获取表的ID并存储在Variable中

时间:2015-07-04 07:55:37

标签: mysql

我想将最后更新的行的id存储到变量中,然后设置触发器来更新一些单元格,触发器的代码:

BEGIN
DECLARE MYROW_ID INT;
DECLARE WITHDRAWAL DECIMAL(11,1);
DECLARE USERNAME VARCHAR(10);
SET MYROW_ID = SELECT id FROM withdrawals ORDER BY lastmodified DESC LIMIT 1;
SET WITHDRAWAL = SELECT withdrawal FROM withdrawals WHERE id = MYROW_ID;
SET USERNAME = SELECT username FROM withdrawals WHERE id = MYROW_ID;
UPDATE  `interests` SET  `totalincome` =  `totalincome` - 'WITHDRAWAL'
WHERE 'username' = 'USERNAME' AND 'status' = 1;
END

MySQL为第5行提供了错误,我是否指定了错误的数据类型,即MYROW_ID
如何存储SELECT into MYROW_ID的返回值?

2 个答案:

答案 0 :(得分:1)

你应该这样做:

SELECT id into MYROW_ID FROM withdrawals ORDER BY lastmodified DESC LIMIT 1;

答案 1 :(得分:1)

我把声明放在括号之间:

SELECT *,
       CASE
         WHEN lead(tag2, 2) OVER w = 'g' THEN lead(token, 2) OVER w
         WHEN lead(tag1) OVER w = 'b' THEN lead(token) OVER w
         ELSE NULL::text
       END AS next_token
FROM my_table
WHERE tag1 = 'a'
  AND next_token IS NOT NULL
WINDOW w AS (PARTITION BY sentence_id ORDER BY id);