I am not sure if its possible or not, Just want to know if it is. I have column plan_popular which has default value 0. Lets same i have a list :
Plan Name | plan_popular | amount
===================================
plan A 0 25.00
plan B 1 50.00
plan C 0 90.00
This is how i am doing:
$stmt = "update {CI}plans set plan_popular = 0";
$this->db->query($stmt);
$stmt2 = "update {CI}plans set plan_popular = 1 where plan_id = ?";
$this->db->query( $stmt2, array($plan_id) );
Now i have set the plan C to make. Now i want to reset it and want to make popular plan C to 1. What i am doing is running two queries, One i reset and make the plan_popular 0 and the second is get the update the plan C to 1 with there id. Is it possible in single query?
You can use an expression to determine the value to assign:
UPDATE {CI}plans
SET plan_popular = IF(plan_id = ?, 1, 0);
try this,
UPDATE {CI}plans
SET `plan_popular` = CASE `Plan Name`
WHEN 'plan C' THEN 1
ELSE 0
END
WHERE `Plan Name` IN((select `Plan Name` from {CI}plans where plan_popular=1 ) , 'plan C');
Updates can be expensive, what with manipulating locks, triggers and constraints firing, etc. In general, you want to avoid updating a field to the same value it already has. In English, if plan_id = variable and plan_popular is 0 then set it to 1 but if plan_id is any other value and plan_popular is 1 then set it to 0.
UPDATE {CI}Plans
SET plan_popular = if( plan_id = ?, 1, 0 )
where (plan_id = ? and plan_popular = 0)
or (plan_id <> ? and plan_popular = 1);
The where clause lets through only those rows that will actually be changed by the update. If this is a largish table, that can make quite a difference in response time. Logic is much less expensive than any actual operation that can performed in the database.
Related
Please how do i update all record to 0 and set only the selected ID to 1
UPDATE address SET default_addr = 1
WHERE addr_id = 100 AND user = 'peter'
The above query will update the selected address to 1 which is good, but i want to set other address or the old selected default to 0 with one query
In MySQL, you can do:
UPDATE address
SET default_addr = (addr_id = 100 AND user = 'peter');
(This shorthand uses the fact that MySQL treats booleans as numbers in a numeric context, with "0" for false and "1" for true.)
If you want only one default address for the user named Peter, then use a where:
UPDATE address
SET default_addr = (addr_id = 100)
WHERE user = 'peter';
I suspect this is the logic that you really want.
use a conditional update using case statement
update address set default_address = case when addr_id = 100 and user = 'peter' then 1 else 0 end
here is a functional example
I built a sample schema. These are often helpful to provide in your future questions.
Update myTable SET field = 1 (if field = 0 or if field is null) where myid = 12345;
Update myTable SET field = 0 (if field = 1) where myid = 12345;
What is the best way to transform this Pseudocode in proper SQL for Oracle and MySQL?
You could simply use the modulo like this:
UPDATE myTable SET field = (field + 1) % 2 WHERE myId = 12345;
Due to the lack of a real boolean in both DBMS you need a case statement:
update myTable
set the_column = case when the_column = 1 then 0 else 1 end
where myId = 12345;
This assumes that the column never has different values than 0 and 1
This is my previous question related to the my query.
MySQL select column which is a value in another column
The problem is that want to do operations on the values extracted and store it back into the original db. I've tried using a update & case but am not able to achieve it.
update msisdn_table
CASE reason
WHEN 'NoAnswer' THEN (case when (NoAnswer>0) then update msisdn_table set NoAnswer = NoAnswer-1 end)
WHEN 'NetworkBusy' THEN (case when NetworkBusy>0 then update msisdn_table set NetworkBusy = NetworkBusy-1 end)
WHEN 'CallRejection' THEN (case when CallRejection>0 then update msisdn_table set CallRejection = CallRejection-1 end)
WHEN 'Unavailable' THEN (case when Unavailable>0 then update msisdn_table set Unavailable = Unavailable-1 end)
END
Any help?
Try it this way if you want to do it one statement
UPDATE msisdn_table
SET NoAnswer = IFNULL(IF(reason = 'NoAnswer',
NULLIF(NoAnswer, 0) - 1, NoAnswer), 0),
NetworkBusy = IFNULL(IF(reason = 'NetworkBusy',
NULLIF(NetworkBusy, 0) - 1, NetworkBusy), 0),
CallRejection = IFNULL(IF(reason = 'CallRejection',
NULLIF(CallRejection, 0) - 1, CallRejection), 0),
Unavailable = IFNULL(IF(reason = 'Unavailable',
NULLIF(Unavailable, 0) - 1, Unavailable), 0)
WHERE reason IN('NoAnswer', 'NetworkBusy', 'CallRejection', 'Unavailable');
Note:
I changed CASE with less verbose IF(), although if you like it better you can use it the same way.
This approach has one possible side effect as it always updates the column(s) either with a new or with old value. It may matter if for example you have a trigger defined on the table.
You want to apply a WHERE clause to make sure that rows with other reason codes are not affected
Here is SQLFiddle demo
update msisdn_table set NoAnswer = NoAnswer-1 where (NoAnswer>0) ;
update msisdn_table set NetworkBusy = NetworkBusy-1 where (NetworkBusy = NetworkBusy-1) ;
update msisdn_table set CallRejection = CallRejection-1 where (CallRejection>0) ;
update msisdn_table set 'Unavailable' = 'Unavailable'-1 where (Unavailable>0) ;
I need to set all values in certain columns...to 1 (where they are now NULL)
Can anyone help out with a little assist on this SQL query/command syntax?
I need to replace several columns where the SchoolID is 184 with a 1, something like this?
SELECT * FROM tblMembers WHERE SchoolID SET column = '1';
You need to use an UPDATE statement:
UPDATE tblMembers
SET column = '1'
WHERE SchoolID = '184'
You can set multiple columns at the same time:
UPDATE tblMembers
SET column = '1', column2 = 'somethingelse', column3 = 'somethingelse'
WHERE SchoolID = '184'
I'm trying to update supplier table on both PostgreSQL and MySQL using the following update statement:
UPDATE SUPPLIERS SET CURDEBT = CURDEBT + 10 WHERE ID = 5
This works fine as long as the CURDEBT column not equals null, if it is null it won't update the record. Does any body have a solution to this problem?
Thanks
In SQL, NULL is not the same thing as 0. Any operations on a NULL value still yield a NULL result. NULL + 10 is still NULL.
If you want NULL to automatically turn into "0" in this query, try this (PostgreSQL):
UPDATE SUPPLIERS SET CURDEBT = coalesce(CURDEBT, 0) + 10 WHERE ID = 5
Or MySQL:
UPDATE SUPPLIERS SET CURDEBT = ifnull(CURDEBT, 0) + 10 WHERE ID = 5
Use coalesce
UPDATE SUPPLIERS SET CURDEBT = coalesce(CURDEBT,0) + 10 WHERE ID = 5
See sqlbook
What you're looking for is COALESCE:
UPDATE SUPPLIERS
SET CURDEBT = COALESCE(CURDEBT, 0) + 10
WHERE ID = 5
Coalesce (MySQL):
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
That behaviour is exactly what you should expect from SQL, since null + x = null, always.
You can solve it by using the COALESCE function, available in both postgres and mysql, like so:
UPDATE SUPPLIERS SET CURDEBT = COALESCE(CURDEBT,0) + 10 WHERE ID = 5