开发者

How to update column while selecting row?

开发者 https://www.devze.com 2023-03-19 04:56 出处:网络
I have a videos table. I want to select a row in table at the same time i want to update views(+1) column in same row.

I have a videos table.

I want to select a row in table at the same time i want to update views(+1) column in same row.

Is here anything possible in 开发者_如何学Pythona single query?


No, you'll have to do that with two queries.


how single the queryis? SELECT * FROM test_table WHERE id=1 ;update test_table set test_col=test_col+1 where id=1;


If You want to do this in a query , you can create trigger on SELECT Like this :

CREATE TRIGGER [TRIGGER NAME] AFTER SELECT ON [TABLE NAME]
  FOR EACH ROW BEGIN
    UPDATE [TABLE NAME] SET [ FIELD ] = [ FIELD ] + 1 WHERE ...... ;
END;

Each time you select from your table , foreach result that matched by your where clause result fields will update


PostgreSQL supports UPDATE ... RETURNING, which would do what you want, but I don't believe MySQL can do this. You will need to do two queries.

0

精彩评论

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