I need to update a MYSQL Table
Here is a very simple look at Table_A
ID VALUE RESULT
1 4 0
2 2 0
3 7 0
I want to update the RESULT Column based on conditions
So my query statement needs to look something like
UPDATE Tabel_A
SET RESULT = (if some condition) 1
OR (if another condition) 2
OR (if a different condition) 3
Or should I use something like
UPDATE Tabel_A
SET RESULT = (CASE 1) 1
(CASE 2) 2
(CASE 3) 3
I am not sure how to structure the query
Thanks
I'll prefer to use CASE here.
UPDATE TAble1
SET Result = CASE value
WHEN 1 THEN x
WHEN 2 THEN y
....
ELSE z
END
or
UPDATE TAble1
SET Result = CASE
WHEN value = 1 THEN x
WHEN value = 2 THEN y
....
ELSE z
END
Related
new to SQL.
I have the following set of data
A X Y Z
1 Wind 1 1
2 Wind 2 1
3 Hail 1 1
4 Flood 1 1
4 Rain 1 1
4 Fire 1 1
I would like to select all distinct 'A' fields where for all rows that contain A have flood and rain.
So in this example, the query would return only the number 4 since for the set of all rows that contain A = 4 we have Flood and Rain.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
Please let me know if you need further clarification.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
You can use aggregation, and filter with a having clause:
select a
from mytable t
where x in ('Flood', 'Rain') -- either one or the other
having count(*) = 2 -- both match
If tuples (a, x) tuples are not unique, then you want having count(distinct x) = 2 instead.
You Shooud use count(distinct X) group by A and having
count(distinct...) avoid situation where you have two time the same value for X
select A
from my_table
WHERE x in ('Flood', 'Rain')
group A
having count(distinct X) = 2
I want to produce an accurate dataset from mysql based on true and false logic, if I have say 4 rows and a column has status, if in the status any row has a zero the data group should be regarded as false else true
e.g
k | s
--------- //This if i group by k, it returns 1 while i
a 0 //want it to return 0 on group
a 0
a 1
a 1
k | s
--------- //This one if i group by k will return 1 which is very true
a 1
a 1
a 1
a 1
Suggestions
Try this query:
SELECT k,
CASE WHEN SUM(s) < COUNT(s) THEN 0 ELSE 1 END AS label
FROM yourTable
GROUP BY k
This would work well assuming that the only values in the s column are 0 or 1. The logic here is that if even a single row has a zero value, then the SUM(s) for a given k group would be less than the number of records in that group.
Scenario:
I have a column in a MySql table:
my_column - [INT] (Unsigned)
What I need:
I need a query to select ONE ENTRY with conditions as follows:
Given A=n
SELECT FIRST the one with my_column = n
ELSE (my_column = n null result)
SELECT the one with my_column = 0
ELSE
SELECT the one with my_column = whatever
ELSE
Return 0 entries
What I looked into:
I tried:
... WHEREmy_columnIN (n,0) ORDER BYmy_columnDESC LIMIT 1
Which applies for the first two steps, but not for the third one.
Thanks for reading.
Given your description, just use case:
order by (case when field = n then 1
when field = 0 then 2
else 3
end)
Then, of course, you would add limit 1.
I am trying to update my table like this:
Update MyTable
SET value = 1
WHERE game_id = 1,
x =-4,
y = 8
SET value = 2
WHERE game_id = 1,
x =-3,
y = 7
SET value = 3
WHERE game_id = 2,
x = 5,
y = 2
I can do a foreach() but that will send over 50 separate Queries which is very slow.
That's why I want it to be combined into 1 big Query.
( I do use an id for each row but the combination of game_id, x and y is what I use to Identify the row I need. )
The update_batch() function from codeIgniter described here:
Update batch with CodeIgniter
was helpful and almost perfect but it only allows for 1 single where clause, you cannot (as far as I understood and tried) enter an array with multiple where clauses.
I've also checked out this question:
MYSQL UPDATE SET on the Same Column but with multiple WHERE Clauses
But it only allows for multiple row updates containing only a single different WHERE clause and I need multiple WHERE clauses! :)
Anwsers can be in simple SQL or with the use of php (and CodeIgniter) or in a different way. I'd this problem to be solved in any possible way ;)
I can really use the advice/help! =D
give this a try by using CASE
Update MyTable
SET value = CASE
WHEN game_id = 1 AND x = -4 AND y = 8 THEN 1
WHEN game_id = 1 AND x = -3 AND y = 7 THEN 2
WHEN game_id = 2 AND x = 5 AND y = 2 THEN 3
ELSE value
END
WHERE game_ID IN (1,2,3) AND -- the purpose of this WHERE clause
x IN (-4, -3, 5) AND -- is to optimize the query by preventing from
y IN (8,7,2) -- performing full table scan.
How do I update a table and set different values upon the condition evaluating to True.
For instance :
UPDATE Table
SET A = '1' IF A > 0 AND A < 1
SET A = '2' IF A > 1 AND A < 2
WHERE A IS NOT NULL;
I have seen CASE expression and IF expression in Procedures and Functions but I want to use it in a simple update/select statement. Is it possible or am I expecting too much from this lovely open source database?
UPDATE table
SET A = IF(A > 0 AND A < 1, 1, IF(A > 1 AND A < 2, 2, A))
WHERE A IS NOT NULL;
you might want to use CEIL() if A is always a floating point value > 0 and <= 2
Whilst you certainly can use MySQL's IF() control flow function as demonstrated by dbemerlin's answer, I suspect it might be a little clearer to the reader (i.e. yourself, and any future developers who might pick up your code in the future) to use a CASE expression instead:
UPDATE Table
SET A = CASE
WHEN A > 0 AND A < 1 THEN 1
WHEN A > 1 AND A < 2 THEN 2
ELSE A
END
WHERE A IS NOT NULL
Of course, in this specific example it's a little wasteful to set A to itself in the ELSE clause—better entirely to filter such conditions from the UPDATE, via the WHERE clause:
UPDATE Table
SET A = CASE
WHEN A > 0 AND A < 1 THEN 1
WHEN A > 1 AND A < 2 THEN 2
END
WHERE (A > 0 AND A < 1) OR (A > 1 AND A < 2)
(The inequalities entail A IS NOT NULL).
Or, if you want the intervals to be closed rather than open (note that this would set values of 0 to 1—if that is undesirable, one could explicitly filter such cases in the WHERE clause, or else add a higher precedence WHEN condition):
UPDATE Table
SET A = CASE
WHEN A BETWEEN 0 AND 1 THEN 1
WHEN A BETWEEN 1 AND 2 THEN 2
END
WHERE A BETWEEN 0 AND 2
Though, as dbmerlin also pointed out, for this specific situation you could consider using CEIL() instead:
UPDATE Table SET A = CEIL(A) WHERE A BETWEEN 0 AND 2
Here's a query to update a table based on a comparison of another table. If record is not found in tableB, it will update the "active" value to "n". If it's found, will set the value to NULL
UPDATE tableA
LEFT JOIN tableB ON tableA.id = tableB.id
SET active = IF(tableB.id IS NULL, 'n', NULL)";
Hope this helps someone else.