Can anybody convert the normal mysql query into codeigniter update strcuture.
UPDATE game_rounds
SET from_score = CASE WHEN from_id = 2 THEN 65 ELSE from_score END,
to_score = CASE WHEN to_id = 2 THEN 65 ELSE to_score END
WHERE round_id=5
I tried update_batch but not able to find correct solution.
Thanks in advance
$this->db
->set('from_score', 'CASE WHEN from_id = 2 THEN 65 ELSE from_score END', FALSE)
->set('to_score', 'CASE WHEN to_id = 2 THEN 65 ELSE to_score END', FALSE)
->where('round_id', 5)
->update('game_rounds');
$this->db->set() enables you to set values for inserts or updates.
It can be used instead of passing a data array directly to the insert or update functions.
The optional third parameter ($escape), that will prevent data from being escaped if set to FALSE.
CI Active Record Class
Here is a simple way to do this
$data['from_score'] = 'CASE WHEN from_id = 2 THEN 65 ELSE from_score END';
$data['to_score'] = 'CASE WHEN to_id = 2 THEN 65 ELSE to_score END ';
$this->db->where('round_id',5);
$this->db->update('game_rounds',$data);
After that run echo $this->db->last_query() to see if it is generating correct query string.
Related
I want To Specfiy two condition In single when statement
for example :
Case
When A.Contract_Payment_Frequency__c = 'MONTHLY'
AND
DATEDIFF(A.loan__First_Installment_Date__c,B.loan__Disbursal_Date__c )
Between
7 AND 35 Then 'TRUE'
When A.Contract_Payment_Frequency__c = 'BI-WEEkLY' and DATEDIFF(A.loan__First_Installment_Date__c,B.loan__Disbursal_Date__c ) Between
7 AND 35 Then 'TRUE'
ELSE 'FALSE'
Im A Beginner CAN YOU Please hel pme
You can mix ANDs and ORs and control what should happen by using appropriate parentheses
Case
When (A.Contract_Payment_Frequency__c = 'MONTHLY' OR
A.Contract_Payment_Frequency__c = 'BI-WEEkLY') AND
DATEDIFF(A.loan__First_Installment_Date__c,B.loan__Disbursal_Date__c ) Between
7 AND 35 Then
'TRUE';
ELSE 'FALSE';
end;
and less verbosely use IN
Case
When (A.Contract_Payment_Frequency__c = IN ('MONTHLY','BI-WEEkLY') ) AND
DATEDIFF(A.loan__First_Installment_Date__c,B.loan__Disbursal_Date__c ) Between
7 AND 35 Then
'TRUE';
ELSE 'FALSE';
end;
I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.
I'm trying to change the values of 150 columns to the following;
'0 = Not provided'
' 1 = Yes '
' 2 = No '
I was able to do this using a case statement for each column. But the problem is it creates puts everything into one column. Is there a way to do it for each individual column without writing 150 case statements? The columns need to be in a specific order.
example:
SELECT CASE
WHEN Answer.Question1_ID is null THEN 'Not Provided'
WHEN Answer.Question1_ID = 1 THEN 'Yes'
WHEN Answer.Question1_ID = 2 Then 'No'
End as 'Question1',
CASE
WHEN Answer.Question2_ID is null THEN 'Not Provided'
WHEN Answer.Question2_ID = 1 THEN 'Yes'
WHEN Answer.Question2_ID = 2 Then 'No'
End as 'Question2'
.
.
.
From Answer
Is there a way to do it for each individual column without writing 150 case statements?
No.
You can use a program to write the case statements if need be.
I'm having issues with writing a case statement in SQL. My first question is: Is it possible to write the if statement below as a case statement in a SQL Query within the select statement?
If no, then please have a look at the case statement below and help/guide me to get into a valid format. Thanks and much appreciated!
IF (var1 = 1){
do this1;
IF (var 1 = 2){
Do this2;
}Else{
do something else1;
}
Else if (Var 1 = 3){
Do this3;
}Else{
Do something else2;
}
Here is my case statement. I know it doesn't work because it's not a valid case statement. Could someone kindly help me in making it a valid case statement. Thanks in advance.
SELECT
CASE
WHEN apple.type = 1 OR apple.type = 2
THEN basket.S1
ELSE
CASE
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
ELSE basket.S2
ELSE
CASE
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END
END
END
FROM .....
WHERE .....
I think you are over complicating your case statement , looking at your first example you case statement should be fairly simple,
something like ....
SELECT CASE
WHEN #Var1 = 1 THEN 'Something 1'
WHEN #Var1 = 2 THEN 'Something 2'
WHEN #Var1 = 3 THEN 'Something 3'
ELSE 'Something Else'
END
FROM .....
WHERE .....
You case statement can we written something like this...
SELECT
CASE
WHEN apple.[type] IN (1,2)
THEN basket.S1
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END
Since you're checking for nulls and substituting non-null values, you can make the query shorter by using the COALESCE function.
SELECT
CASE
WHEN apple.type IN (1, 2) THEN basket.s1
WHEN apple.type = 0 THEN COALESCE(basket.s2, basket.s1)
WHEN apple.type = 3 THEN COALESCE(basket.s3, basket.s1)
END
FROM ...
I have a table that, due to the third party system we are using, sometimes has duplicate data. Since the model uses an EAV method there's no way to filter this the "right" way, so I am aggregating the data into a View - I know this is a data collection problem but it's easier for me to fix it on the display end than go through this system and potentially break existing data and forms. I need to check one of two fields to see if one or both are entered, but only pick one (otherwise the name displays twice like this: "John,John" instead of just "John"). Here's my code for the relevant part:
group_concat(
(
case when (`s`.`fieldid` = 2) then `s`.`data`
else
case when (`s`.`fieldid` = 35) then `s`.`data`
else NULL end
end
) separator ','),_utf8'') as first_name
If both fieldid 2 and fieldid 35 are entered, I would expect this to just return the value from fieldid = 2 and not the value from fieldid = 35, since the Else clause shouldn't execute when the original case when is true. However it's grabbing that, and then still executing the case when inside of the else clause?
How can I fix this code to give me either fieldid = 2 OR fieldid = 35, but avoid globbing them both together which results in the name being duplicated?
EDIT
Here is the table structure:
table: subscribers_data
subscriberid (int) fieldid (int) data (text)
It uses an E-A-V structure so a sample record might be:
subscriberid fieldid data
1 2 John
1 3 Smith
1 35 John
1 36 Smith
with fieldid 2 and 35 being the custom field "First Name" (defined in a separate table) and fieldid 3 and 36 being "Last Name".
Here is the full view that I'm using:
select `ls`.`subscriberid` AS `id`,
left(`l`.`name`,(locate(_utf8'_',`l`.`name`) - 1)) AS `user_id`,
ifnull(group_concat((
case when (`s`.`fieldid` = 2) then `s`.`data`
when (`s`.`fieldid` = 35) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `first_name`,
ifnull(group_concat((
case when (`s`.`fieldid` = 3) then `s`.`data`
when (`s`.`fieldid` = 36) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `last_name`,
ifnull(`ls`.`emailaddress`,_utf8'') AS `email_address`,
ifnull(group_concat((
case when (`s`.`fieldid` = 81) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `mobile_phone`,
ifnull(group_concat((
case when (`s`.`fieldid` = 100) then `s`.`data`
else NULL end) separator ','),_utf8'') AS `sms_only`
from ((`list_subscribers` `ls`
join `lists` `l` on((`ls`.`listid` = `l`.`listid`)))
left join `subscribers_data` `s` on((`ls`.`subscriberid` = `s`.`subscriberid`)))
where (left(`l`.`name`,(locate(_utf8'_',`l`.`name`) - 1)) regexp _utf8'[[:digit:]]+')
group by `ls`.`subscriberid`,`l`.`name`,`ls`.`emailaddress`
The view is being used as the Model for a Ruby on Rails application, so I'm using some creative hacking to fake out a "user_id" that Rails expects (we name the field list.name in the Lists table using a numeric ID that our front-end Rails app generates when we add a new user, so I'm extracting just this number to make the view look like a Rails-convention database table)
I am not a mysql guy, but in a sql server case statement, you could do it without the first 'else'
case
when fieldid = 2 then data
when fieldid = 35 then data
else null
end
Also, you seem to be returning the same 'data' field in both cases
Anything inside group_concat() doesn't have a way to see the context in which it's running. So, you have have two rows in a single group, one with fieldid=2 and second with fieldid=35, it will do the following:
processing row with fieldid=2...
s.fieldid = 2 is true, return s.data
processing row with fieldid=35...
s.fieldid = 2 is false, try the else part
s.fieldid = 35 is true, return s.data
This explains why is "John" returned multiple times. The only way to fix it is to run a different query outside of group_concat().
EDIT:
Ih you really have to do it this way, use something like this instead:
SELECT ...
min(CASE WHEN s.fieldid IN (2,35) THEN s.data ELSE NULL END) AS first_name
...
Alternatively you can do group_concat(DISTINCT ...) if the two values can't be different (otherwise you would get e.g. "John,Johny"). Why do you have two values for first_name/last_name though?