hyunwookcho
hyunwookcho

Reputation: 157

In DB table, How to update query after select query?

I want update query after select query.

first SELECT query , SELECT * from be_settings order by seq desc limit 1

this select query is based on seq, retrieve the last inserted record.

I want update on this record.

second Update Query, UPDATE be_settings set appgubun ='CCTV', running ='on'.

how to update query after select query?

thanks.

Upvotes: 0

Views: 46

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521864

You can do this via an update with a subquery:

UPDATE be_settings
SET appgubun = 'CCTV', running = 'on'
WHERE seq = (SELECT t.max_seq FROM (SELECT MAX(seq) AS max_seq FROM be_settings) t );

The subquery in the WHERE clause is necessary because it involves the be_settings table, which is the target of the update. The following would give an error:

WHERE seq = (SELECT MAX(seq) FROM be_settings)

Upvotes: 1

Yogs
Yogs

Reputation: 80

You need to do

UPDATE be_settings set appgubun ='CCTV', running ='on' where  seq= '4'

Assuming your there is seq column on be_settings and it is primary key.

and 4 is the last inserted id you are getting from select query.

if you are getting that seq in any variable based on programming language then you have to use that particular variable.

You have to this if you do not want to use subquery.

Upvotes: 0

Related Questions