SQL - Set column value depending on another column value - mysql

I have my database with the following key columns
What I would like to do is using the information provided create 2 new columns that contains a value based on 2 other columns.
1) A 'win' column - This is if 'pos' = 1 then value will be (BSP - 1) - EG One for Billy will read 1.06 else it will read "-1"
2)A 'diditplace' Column - this is if 'Placed' = 1 then value will be (place - 1) - EG One for Billy will read 0.27 else it will read "-1"

Try this:
UPDATE myTable
SET newColumn1 = CASE pos WHEN 1 THEN 'BSCP - 1' ELSE '-1' END,
newColumn2 = CASE placeId WHEN 1 THEN 'place - 1' ELSE '-1' END
If you want to add new columns pragmatically add this before:
ALTER myTable
ADD COLUMN newColumn1 VARCHAR(256) NOT NULL,
ADD COLUMN newColumn2 VARCHAR(256) NOT NULL
Replace newColumn1 and newColumn2 with your desired column names.

Related

MYSQL- Multiple column value change

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.

SQLite Query SET variable on a single row

I have a table called "allarmi_ingressi" in SQLite, with a lot of rows in it.
I want to create a query that changes the variable on my column "visto" to 1, if "visto=0", and to 0, if "visto=1".
This is what i made:
UPDATE allarmi_ingressi SET visto = '1' WHERE visto = '0'
Of course this modify every row in the column "visto";
I want to know if it's possible to modify it "selecting" it by the primary key, in my case "id_allarme".
In a SELECT query, you would use the WHERE clause to find rows with a specific id_allarme value.
The same WHERE clause can be used with UPDATE:
UPDATE allarmi_ingressi
SET visto = 1 - visto
WHERE id_allarme = ?;
Use CASE Expression
Query
update allarmi_ingressi
set visto = (
case visto when '1' then '0'
when '0' then '1'
else visto end
)
where id_allarme = __; -- id here

Check field value, do a calculation (division) and update column values

I need to make some calculation in MySQL table which contains hundreds of rows.
I've got a field called "VAT-Code" which contain a code : 1 or 2
I've got a field called "Price" which contain the price
Query should do the following :
If "VAT-Code" value = 1 calculate "Price" / 1,08 -> update the field value
If "VAT-Code" value = 2 calculate "Price" / 1,025 -> update the field value
Ok I have tried this :
I have tried this :
UPDATE jos_tempcsv SET selling price = CASE `VAT-Code` WHEN = 1 THEN `selling price`/1.08 WHEN = 2 THEN `selling price`/1.025 ELSE NULL END
selling price is the field with the price
But I get the following error :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'price = CASE VAT-Code WHEN = 1 THEN selling price/1.08 WHEN = 2 THEN `sellin' at line 1
You can run UPDATE with CASE:
UPDATE yourtable SET yourfield=CASE `VAT-Code` WHEN 1 THEN `Price`/1.08 WHEN 2 THEN `Price`/1.025 ELSE NULL END;
Or you can run this as two separate UPDATEs:
UPDATE yourtable SET yourfield=`Price`/1.08 WHERE `VAT-Code`=1;
UPDATE yourtable SET yourfield=`Price`/1.025 WHERE `VAT-Code`=2;
You can try the below sql query :
UPDATE yourtable table1,(SELECT * FROM `yourtable` WHERE VAT-Code = 1) table2 SET table1.yourfield = (table2.price/1.08) WHERE table1.VAT-Code = 1
UPDATE yourtable table1,(SELECT * FROM `yourtable` WHERE VAT-Code = 2) table2 SET table1.yourfield = (table2.price/1.025) WHERE table1.VAT-Code = 1
This query works fine now :
UPDATE jos_tempcsv SET `selling price` = `selling price`/1.08 WHERE `VAT-Code`=1;
UPDATE jos_tempcsv SET `selling price` = `selling price`/1.025 WHERE `VAT-Code`=2;
Two queries in one
UPDATE jos_tempcsv SET `selling price` = CASE `VAT-Code` WHEN 1 THEN `selling price`/1.08 WHEN 2 THEN `selling price`/1.025 ELSE NULL END;

SQL - decrease value to zero

I know how to increase/decrease value in column.
value type is INT(11)
Command:
UPDATE table SET value = value - 1 WHERE id = 1
But when value reaches "0" it keeps decreasing.
Question: How to decrease until zero, so I don't get value (-1) in column?
Thank you for your code / ideas / suggestions
Just use a conditional:
UPDATE table
SET value = value - 1
WHERE id = 1 AND value > 0;
Add it to the where clause:
UPDATE table SET value = value - 1 WHERE (id = 1) AND (value > 0)
Once value reaches 0, it won't get updated anymore, because the where clause will exclude that record from the update.
You can add on to your where clause like so:
UPDATE table
SET value = value - 1
WHERE id = 1 and value >= 1
You can use CASE.
UPDATE table SET value = CASE WHEN value = 1 THEN value = value - 1
ELSE value
END
WHERE id = 1

MySQL CASE "Else Case When" is executing when the precondition is true - what am I missing?

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?