MySQL : Comparing values of two different colums from a same table - mysql

I would like compare the value of two colums from a single table, and UPDATE another colums in function of results, in MySql. Values are on the same row.
Example:
If value COL A > value Col B ==> Col C = "player1"
If value COL A < value Col B ==> Col C = "player2"
How could i do that ?
Something like :
UPDATE table SET col C = "player1" WHERE ...
Edit :
So i just try with CASE...
UPDATE partie SET col C =
CASE
WHEN Col A > Col B THEN 'player1'
WHEN Col A < Col B THEN 'player2'
ELSE 'deuce'
END
WHERE .... ;
Is it correct in Mysql ?
Many thanks

Either do so in two different queries:
UPDATE table SET C = 'player1' WHERE a > b;
UPDATE table SET C = 'player2' WHERE a < b;
Or try this one liner:
UPDATE table SET C = IF(a > b, 'player1', 'player2')
Do note:
What do you want to happen when a = b?
Without further filtering all of the above will make for full table scans, so very heavyweight for large tables.

Something like this
<?php
if ($Cola > $Colb){
UPDATE $tableName SET ColC = "player1";
}
else{
UPDATE $tableName SET ColC = "Player 2";
}
?>

Related

Can you re-use update values for on duplicate key updates?

So let's say you have column A,B,C. On duplicate key, let's say you do A = {some statement here}, B = {some statement here}, and C= New_A + New_B. Can I use what would be the new values of A and B in order to determine the new value of C, or do I have to retype the expressions for the new A and B? Thanks!
I think you can do it. If you do:
ON DUPLICATE KEY UPDATE
A = A + 1, B = B * 2,
C = A + B
I believe the updates are executed left to right. So when it gets to C = A + B, A and B contain the new values.

UPDATE query for multiple rows of same column

I have a database table and What I required to do is that,
I need to update the column with the column name 'Co15' of every rows according to the following conditions
Co15 = SAMPLE if Co13 = 'c1' AND Col2 = 'b4'
Co15 = LIST if Co13 = 'c6'
Currently I am running each update query separately as follows
UPDATE tblname SET Co15 = 'SAMPLE' WHERE Co13 = 'c1' AND Col2 = 'b4';
UPDATE tblname SET Co15 = 'LIST' WHERE Co13 = 'c6';
But wanted to know if there is any way where I could run only one update query all at once.
Thanks
Try this
UPDATE tblname SET Co15=
CASE
WHEN Co13 = 'c1' AND Col2='b4' THEN 'SAMPLE'
WHEN Co13 = 'c6' THEN 'LIST'
END
exactly getting the output to as following as:
UPDATE tblname
SET col5= CASE
WHEN col3 = 'c1' AND col2 = 'b4' THEN 'SAMPL'
WHEN col3 = 'c6' THEN 'LIST'
END
example: sqlfiddle to click here

How to update value in MySQL when the stored version differs to the passed argument

UPDATE table SET x = :x, y = :y, z = IF(x <> :x, NOW(), z);
I need to check whether value of the old "x" is different from the new "x". I can't do that within the IF statement as shown above because value of x is already changed so it equals to the :x.
Basically, what I am trying to do is something like:
IF(oldX <> :newX) THEN NOW() ELSE NO CHANGE;
Is there any way how to achieve that or am I better doing this using a subquery or a variable? *I definitely prefer executing one query only.
MySQL does not support the standard on this:
The second assignment in the following statement sets col2 to the
current (updated) col1 value, not the original col1 value. The result
is that col1 and col2 have the same value. This behavior differs from
standard SQL.
UPDATE t1 SET col1 = col1 + 1, col2 = col1;
So, the following works in a simple example in SQL Fiddle:
UPDATE table
SET x = if (#x := x, :x, :x),
y = :y,
z = IF(x <> #x, NOW(), z);
It should also work if you reverse the order:
UPDATE table
SET z = IF(x <> :x, NOW(), z),
x = :x,
y = :y;
The documentation says that single table updates are "generally" processed in lexical order.

Update table using alias

I need to fill some fields in a table getting informations from other records of the same table.
I tried to write a query to explain what I want to do:
update globale2
set
nita = t.nita,
tita = t.tita,
notaita = t.notaita
where
neng = t.neng and
nita is null
(select nita, neng, tita, notaita from globale where uris='mma' and nita is not null) as t
edit to eplain better:
every records have these fields: "nita", "tita", "notaita", "neng" ("neng" cannot be null)
I want to fill these fields: "nita", "tita", "notaita" (where "nita" is empty)
with the same values from another record where "neng" equals the other "neng"
You can however, join the two tables.
UPDATE globale2 g
INNER JOIN globale gg
ON g.neng = gg.neng
SET g.nita = gg.nita,
g.tita = gg.tita,
g.notaita = gg.notaita
WHERE g.nita IS NULL
AND gg.uris = 'mma'
AND gg.nita IS NOT NULL
assume there is a table A_temp, with two columns 'one' and 'two'.
TABLE A_temp
ONE TWO
1 2
this is the present status of the table.
The query
UPDATE (SELECT * FROM A_temp ) A SET one = A.two where one = '1'
updates the table as
ONE TWO
2 2
Hope you get the idea and that it helps..

Update column depending on other column by calculation

seems like a stupid question...
I have a mysql table where I want to modify column A to a number 0 or 1 depending on the condition of another column B
So: if( B > 500 ) A = 1 ELSE A = 0
Column A = INT
Column B = DOUBLE
How do you do something like this in sql?
Thanks,
Erik
Try the following statement,
UPDATE tableName
SET A = (B > 500)
SQLFiddle Demo
(B > 500) is a boolean arithmetic in mysql which returns 1 and 0 for true and false , respectively.
You can also use CASE for much more RDBMS friendly,
UPDATE tableName
SET A = CASE WHEN B > 500 THEN 1 ELSE 0 END