2

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'
3
  • Please elaborate on the need to combine. Perhaps you are getting all the columns (*)? Or you don't like to issue two commands? Or something else?
    – Rick James
    Aug 26, 2018 at 21:38
  • very often this bundle is used and I thought it might be possible to increase the productivity Sep 30, 2018 at 19:26
  • Will you be performing those two commands more than a thousand times per second? If not, then don't worry.
    – Rick James
    Sep 30, 2018 at 19:30

1 Answer 1

1

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;
1
  • These seem incomplete. What is the set of columns indicated by *?
    – Rick James
    Sep 30, 2018 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.