MySQL - How to change value in column? - mysql

I have a OpenCart store with more than 23000 products and I need to change one option (Requeres Shipping) for all of my products. I need to change that option in my database. I have a table n0v_product with with column shipping with value 0. I need to change shipping value to 1 for all products. How to update the value in the column with phpMyAdmin?

Run this in a test database before running it on production but this should do the job.
UPDATE n0v_product
SET shipping = '1'
WHERE shipping = '0'

You can use a UPDATE command to change all values on column shipping to 1 on n0v_product:
UPDATE `n0v_product` SET `shipping` = 1
If you only want to set the value if shipping is 0 you can use the following:
UPDATE `n0v_product` SET `shipping` = 1 WHERE `shipping` = 0
You should check the rows before UPDATE to make sure you really want to UPDATE these rows:
-- all rows of table n0v_product
SELECT `shipping`, * FROM `n0v_product`
-- only rows with shipping = 0
SELECT `shipping`, * FROM `n0v_product` WHERE `shipping` = 0

Related

How can i run a select and update query simultaneously from phpmyadmin

I have a scenerio. I have a field called card_no. Some entries are blank. We don't want to handle that. Some have 16 digit integers and some have the proper data I need. What I need is that I want to select all the records that are 16 digits integer and replace them with xxxxxxxxxxxxLAST4DIGITS (The credit card format). Can i do that via mysql?
You don't have to SELECT the rows.
You can apply an UPDATE to a subset of rows matching a condition.
UPDATE scenario
SET card_no = CONCAT('xxxxxxxxxxxx', RIGHT(card_no, 4))
WHERE LENGTH(card_no) = 16
Yes. But you have to have this field as char(4) as you might want to store a number 0002 as 0002 not just 2. So I would first update the datatype of the field and then update the field entries as desired.
-- update datatype
alter table `cards`
modify column `card_no` char(4);
-- update values in card_no
update `cards`
set `card_no` = LPAD(if(length(`card_no`) > 4, substr(`card_no`, -4, 4), `card_no`), 4, '0')
where `card_no` is not null;
The 'xxx' append operation you can do at application code side or else it will take extra memory in db.

UPDATE (B) from (A) if (B) = 0?

I'm learning this for school and I'm confused. I'm trying to copy information between columns in a single table in a single db, all local.
Basically:
(I need to loop through and update all records)
UPDATE `my_records`
SET `realname` = `name`
WHERE `realname` = 0;
SELECT * FROM `my_records` SET `realname` = `name` WHERE `realname` = 0;
It keeps telling me I have a syntax error.
I now see why they are asking me to learn this. Each row in the table is different so when I update all columns some rows change that shouldn't so that's not the end result I'm after. I can try to give an example but this is confusing to me.
DB -> Table -> Row 1 - holds the name of the person -> Row 2 - holds the picture
of the person
Both things have a name (example Row 1 David, Row 2 Flower.JPG)
So I'm guessing they want me to figure out a way to exclude updating the 'real_name' column on Row 2 where the image is a JPG, GIF, or PNG.
I think the final result they are looking for when the table is updated is:
Row 1 'David' 'David'
Row 2 'flower.jpg'
Then this loops over and over again for all the records.
You need to use an UPDATE instead of a SELECT. SELECT statements only return data, they do not modify data.
So, to return the records you will update in the next step:
SELECT `realname`, `name` FROM `my_records` WHERE `realname` = 0;
and then to update those records:
UPDATE `my_records` SET `realname` = `name` WHERE `realname` = 0;
Note that this query will update the entire table, setting any row where the value of realname is equivalent to 0, to the value of that same row's name column.
A few other possibly useful statements:
UPDATE `my_records` SET `realname` = `name` WHERE `realname` = '';
This will affect all rows where realname is equivalent to 'empty string'
UPDATE `my_records` SET `realname` = `name` WHERE `realname` IS NULL;
will affect all rows where realname is NULL

How to update multiple rows with one query with MySQL?

I'm currently running more database queries of update, like the following:
UPDATE table SET status = 1 WHERE id = 3
UPDATE table SET status = 1 WHERE id = 7
UPDATE table SET status = 1 WHERE id = 9
UPDATE table SET status = 1 WHERE id = 18
etc...
Question:
How is it possible to run these queries in one?
UPDATE table SET status = 1 WHERE id in (3,7,9,18,...)
If you need to update some rows on a given list you can use IN()
UPDATE table SET status = 1 WHERE id IN (3, 7, 18);
If instead you need to update all rows just don't add any WHERE conditions
UPDATE table SET status = 1;
Your question is a bit general if you mean how to update multiple rows in one command in general it depends on your queries but if your question is more specific and you need to run 1 single query instead of all above queries you can try this :
UPDATE table SET status = 1 WHERE id IN (3,7,9,18)

MySQL UPDATE Based off of values

I need a query to update rows but first check if there a specific number.
Example:
number = 10
If the row includes the number 10, don't update.
It needs to be just the query. Something like this.
SELECT `number` FROM `users`;
if row.number == 10 then don't update
else update set number=12
UPDATE users
SET number = 12
WHERE number <> 10

re-update sql trouble

I just this error when Im updating database and I dont change any of the values in the form and submit. I would like to know why and suggestion to correct this error.
sample
SELECT * FROM `table` WHERE `id` = 1
id = 1,
name = John,
city = New York;
UPDATE `table` SET name = 'John', city = 'New York' WHERE id = 1
when updating the database with the same values what you select from database and use affected rows i get 0
Enclose the string values in quotes (')
UPDATE `table` SET name = 'John', city = 'New York' WHERE id = 1
As the manual states:
For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.
Therefore, because your UPDATE does not actually change any rows, the default affected-rows value is 0.