Why UPDATE query not working - mysql

Here is my code
UPDATE project SET name = 'New Name' AND platform = 'iOS' WHERE id = 2
When I process it via phpMyAdmin, it is changing the name to 0. When I rerun the query, it says 0 rows affected. When I change back the name to something else and rerun the query, it says 1 row affected, but changing the name back to 0 and nothing else.
What am I doing wrong?
I have the table with proper structure and have a record with id=2

Remove AND and use , instead
UPDATE project SET name = 'New Name', platform = 'iOS' WHERE id = 2

Your UPDATE syntax is wrong. It should be this:
UPDATE project SET name = 'New Name', platform = 'iOS' WHERE id = 2
MySQL is changing name to 0 because it is confused about what you mean by AND. It's possible MySQL is interpreting that statement to be a logical expression, which gets converted into a 1 for TRUE or a 0 for FALSE (in this case, the latter).
Think of your original query like this, with parentheses used to illustrate operator precedence:
UPDATE project SET name = ('New Name' AND (platform = 'iOS')) WHERE id = 2

UPDATE project SET name = 'New Name', platform = 'iOS' WHERE id = 2

Depending on what you want to do, correct syntax is
UPDATE project SET name = 'New Name', platform = 'iOS' WHERE id = 2;
Or
UPDATE project SET name = 'New Name' WHERE id = 2 AND platform = 'IOS';

Related

Select Case / Update one field based on another

Basically, I am trying to cleanse a database and have all file paths point to sample documents. I am reading from one column (extension), and then updating another column (filename) and making a change based on the extension. I also want a catch all. For example, if extension is DOC, then make the path "sample.doc"; if the extension is PDF, then make the path "sample.pdf", if unknown, then make the path "sample.txt".
I have this code working, but think it would be better served with a select/case loop:
select * from dbo.tsrecelec
UPDATE dbo.TSRECELEC
SET dbo.tsrecelec.reSID = '00+SampleWord.doc'
WHERE dbo.tsrecelec.reExtension = 'DOC'
UPDATE dbo.TSRECELEC
SET dbo.tsrecelec.reSID = '00+SampleWord.docx'
WHERE dbo.tsrecelec.reExtension = 'DOCX'
UPDATE dbo.TSRECELEC
SET dbo.tsrecelec.reSID = '00+SampleADOBE.pdf'
WHERE dbo.tsrecelec.reExtension = 'PDF'
In my NONE SQL brain, I was thinking something like this might work?
SELECT reSID, reExtension,
CASE
WHEN reExtension = 'DOC' THEN SET reSID = '00+SampleWord.doc'
WHEN dbo.tsrecelec.reExtension = 'DOCX' THEN SET dbo.tsrecelec.reSID = '00+SampleWord.docx'
ELSE dbo.tsrecelec.reSID = '00+SampleFile.txt'
END
FROM dbo.tsrecelec;
Any and all help is appreciated!
Three updates is fine, but you can phrase this as:
UPDATE dbo.TSRECELEC
SET reSID = (CASE WHEN reExtension = 'DOC'
THEN '00+SampleWord.doc'
WHEN reExtension = 'DOCX'
THEN '00+SampleWord.docx'
WHEN reExtension = 'PDF'
THEN '00+SampleADOBE.pdf'
END)
WHERE reExtension IN ('DOC', 'DOCX, 'PDF');
Try to use CASE...WHEN...THEN.... It's more performant if you are getting more extensions in the future:
UPDATE yourtable
SET col1 = (CASE WHEN col2 = 'extension you want'
THEN 'nameofyourfile.extension'
WHEN ...
THEN ...
END)
WHERE col2 IN ('extension1', 'extension2', 'extension3','...');

Update database record by selected id and reset the initial record

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 Same mysql field twice in single query

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.

Basic logic in MySQL

Our current mysql script that connects our Invoicing software to our website updates stock levels and what not, but there is a field in our products table which dictates if the product is visible or not which the script does not address. I want to introduce some IF logic to set the prodvisible column to 1 IF the stock level it's being updated with is > 0.
In my research, it appears that IF's cannot appear outwith functions, sadly something I have no experience of and despite my best efforts I can't get it to work.
The current script we have which works succesfully to update stock levels is as follows...
update isc_products p
set
p.prodcurrentinv =[{Level_LessOrderBook}]
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
--GO;--
update isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where pvc.vcsku='[{ItemNumber}]'
I want to integrate something into the first section that does something like the following
If [{Level_LessOrderBook}] > 0
p.prodvisible = 1 where p.prodcode = '[{ItemNumber}]'
ENDIF
I don't want it to set the product to invisible if it it's out of stock, just visible if it's in stock.
Thanks for any help.
You should be able to do this without an IF statement:
update isc_products p
set p.prodvisible = 1
where p.prodcode = '[{ItemNumber}]'
and [{Level_LessOrderBook}] > 0
Or, if you were asking about doing it in one statement:
update isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = IF([{Level_LessOrderBook}] > 0, 1, p.prodvisible)
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
Finally, how about this?
update
isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = case when [{Level_LessOrderBook}] > 0 then 1 else p.prodvisible end
where
p.prodcode = '[{ItemNumber}]'
and p.prodinvtrack = 1
--GO;--
update
isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where
pvc.vcsku='[{ItemNumber}]'
I formatted everything in exactly the same way as the rest of your existing script. Are you sure that the column prodvisible exists, is spelled correctly, and takes a numeric or bit value?

ms access if in sql

I am trying to update the column called NewNumber. What I want to do is if the column phone_code has 1 in it, I want to update the column NewNumber with the value of the phone_number column. But if it is anything else than 1 then I want to concatenate the phone_code and phone_number columns and update NewNumber column. Below is the code I tried but that doesnt work
update Arvada_N set NewNumber = IIf([phone_code = 1], phone_number, [phone_code&phone_number]);
Any help will be appreciated
Never mind, this works
update Arvada_N set NewNumber = IIf([phone_code] = '1', phone_number, [phone_code]&[phone_number]);