0

I want to update the table.I have two update query but I want to use it in a single update query.Column name and Where condition are different in both queries.Would you help me in this?

   $sql="UPDATE points SET hero='$total_hero'+'$points' WHERE user_id='$hero_id'"; 
   $sql="UPDATE points SET zero='$total_zero'+'$points' WHERE user_id='$zero_id'";
Kilian Stinson
  • 2,376
  • 28
  • 33
  • It is possible, but nothing wrong with doing it in 2 queries – juergen d Jan 10 '17 at 13:43
  • Duplicate of http://stackoverflow.com/questions/3432/multiple-updates-in-mysql – German Lashevich Jan 10 '17 at 13:45
  • but it is working only single query and that is the second one. First query is not working. I am using in else { $sql="UPDATE points SET hero='$total_hero'+'$points' WHERE user_id='$hero_id'"; $sql="UPDATE points SET zero='$total_zero'+'$points' WHERE user_id='$zero_id'";} –  Jan 10 '17 at 13:46
  • Mr.German, I checked that, That is different question and query is also different –  Jan 10 '17 at 13:47
  • In your example code you are simply overwriting your string so the first query will never actually execute. – jeroen Jan 10 '17 at 13:48
  • because, your second took precedence over your first; use a mutli-query. Edit: @jeroen GMTA, I actually hesitated before hitting that submit button lol – Funk Forty Niner Jan 10 '17 at 13:48
  • @Fred-ii- That explains the 2-second difference ;-) – jeroen Jan 10 '17 at 13:49
  • Possible duplicate of [How to execute two mysql queries as one in PHP/MYSQL?](http://stackoverflow.com/questions/802437/how-to-execute-two-mysql-queries-as-one-in-php-mysql) – Funk Forty Niner Jan 10 '17 at 13:50
  • @jeroen sure does ^ – Funk Forty Niner Jan 10 '17 at 13:50
  • Thanks for replying Mr.Jeroen and Fred, Can you help me with single update query? –  Jan 10 '17 at 13:50

1 Answers1

0

You can make use of the below query

$sql="UPDATE points 
      SET hero= CASE WHEN user_id='$hero_id' THEN '$total_hero'+'$points' ELSE hero END, 
      zero= CASE WHEN user_id='$zero_id' THEN '$total_zero'+'$points' ELSE zero END 
      WHERE user_id='$hero_id' OR user_id='$zero_id'"; 

Hope this would help you out.

Viki888
  • 2,686
  • 2
  • 13
  • 16
  • Thanks for replying Mr.Viki888. It's working for me perfectly.upvote from my side –  Jan 10 '17 at 14:03