How to get mysql update from select?

552    Asked by Ethan Clark in SQL Server , Asked on Oct 3, 2022

 Here are two SQL statements. One is a select statement and the other is an update statement. I want to combine them.

Select

SELECT * FROM test WHERE `id`='1'

Update

UPDATE test SET `select_count`=`select_count`+'1' WHERE `id`='1'


Answered by Faith Davidson

Based on your question - How to get mysql update from select -


 I think you need to include SELECT statement in UPDATE statement:

Sample SQL in below:

create table test1

(ID int,

select_count int,

select_name varchar(50))

Insert into test1 values (1,1,'A')

Insert into test1 values (2,2,'B')

UPDATE test1

SET select_count = select_count + 1

where select_count = (SELECT select_count FROM test1 WHERE select_name = 'A')

Another way: If you have 2 tables (TableA and TableB)

UPDATE TableB

SET TableB.value = (

    SELECT TableA.value

    FROM TableA

    WHERE TableA.name = TableB.name

)

where TableB.value < X>



Your Answer