Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Update query with multiple values

Tags:

sql

mysql

I have a table in the database with records like following:

match_id | guess | result
   125   |   1   |   0
   130   |   5   |   0
   233   |   11  |   0
   125   |   2   |   0

my users choose a guess for each match and I have a function that calculate the result of the guess depending on the result of the match: if the guess is right the result will be (1) if it is wrong the result will be (2) if the match did not finish yet the result will be (0 default) I have eleven possibilities for guesses (more than one could be right at the same time) for example : if I have a match whit id=125 and I have all the guesses wrong except 8,11 so i should update the result field for all matches that have the match id and their guess is 8 or 11 (i will give 1 for this result field) and I want to give (2) for the other guesses of the same match

I use this query for all eleven possibilities like following:

UPDATE `tahminler` SET result=1 WHERE match_id='1640482' AND tahmin='8'
UPDATE `tahminler` SET result=1 WHERE match_id='1640482' AND tahmin='11'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='1'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='2'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='3'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='4'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='5'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='6'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='7'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='9'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='10'

I want to know if I can do this job in one query?or not?

like image 975
Basel Avatar asked Nov 01 '13 12:11

Basel


People also ask

How UPDATE multiple columns with different values in SQL?

To update multiple columns use the SET clause to specify additional columns. Just like with the single columns you specify a column and its new value, then another set of column and values. In this case each column is separated with a column.

How do I UPDATE multiple values in MySQL?

MySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.


1 Answers

use these two query:

UPDATE `tahminler` 
        SET result=0 
        WHERE match_id='1640482' 
              AND tahmin IN ('1','2','3','4','5','6','7','9','10')

And then use this:

UPDATE `tahminler` 
        SET result=1 
        WHERE match_id='1640482' 
              AND tahmin IN ('8','11')
like image 60
Code Lღver Avatar answered Oct 12 '22 23:10

Code Lღver