Thesql update based on select result, How combine

MySQLselectupdate

Here are two SQL statements. One is an select statement and the other is a 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'

Best Answer

Based on your question 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;