-1

I'm trying to get the following to run via a single pdo statement

UPDATE `coin_price` SET `coin_id` = 1 WHERE coin_id = 1; UPDATE `coin_price` SET `coin_id` = 178 WHERE coin_id = 178; 

and I am getting the following exception

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE `coin_price` SET `coin_id` = 178 WHERE coin_id = 178'` at line 1

I am just running

$statement = $this->system[$location]["connection"]->prepare($sql); $statement->execute(); 

and getting an exception. I tried with binding and without.

Is what I am doing not possible?

Note : Both queries run fine via PDO by themselves

6
  • 3
    Why are you setting coin_id to the same value it already has? Commented Jul 14, 2020 at 22:59
  • stackoverflow.com/a/3466/642096 Commented Jul 14, 2020 at 23:06
  • There is no point running multiple update queries when you can handle it in one. Also setting coin_id to a value it already is assigned doesn't makes sense. Commented Jul 15, 2020 at 2:05
  • This is just an example query I'm using for testing Commented Jul 15, 2020 at 3:03
  • I don't want to use ON DUPLICATE KEY as I would like to maintain incremental IDs @cetver Commented Jul 15, 2020 at 3:04

2 Answers 2

2

Don't try to run multiple statements at once.

Run them separately. But, if you need transactional semantics, surround by START TRANSACTION and COMMIT. (Example: You don't want a crash between debiting one financial account and crediting another.)

Another plan is to write and CALL a Stored Procedure to combine the statements.

If you are concerned about performance, well, you are talking about very few milliseconds.

Sign up to request clarification or add additional context in comments.

Comments

1

You can only execute one query at a time. But you can combine them into a single query:

UPDATE coin_price SET coin_id = CASE coin_id WHEN 1 THEN 1 WHEN 178 THEN 178 END WHERE coin_id IN (1, 178) 

3 Comments

Thank you for your reply. Is it possible to expand this to update as many fields as wanted?
Yes, you can assign multiple columns, and each of them can contain a CASE expression to assign the appropriate value.
See the duplicate question for examples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.