MySql update Query For Multiple rows Update - mysql

I have value in column seq_id = {18,19,20,21,22,23,24}
I want to update this by {19,20,21,22,23,24,18}
I want to update this column only .other columns remain as it is.
How can i update this in mysql query.

Try this:
UPDATE yourTable
SET seq_id = seq_id + 1
WHERE seq_id BETWEEN 18 AND 23
UPDATE yourTable
SET seq_id = 18
WHERE seq_id = 24

Related

SQL update where select from the same table [duplicate]

I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo Name Quali Exp
1 x B.E 2
2 y BSC 3
3 Z B.A 1.5
4 A MSC 2
5 B MBA 5
Here i want to update second row with the value of 5th row.
Here is my current query:
UPDATE table
SET Name=(select Name from table where slNo='5'),
Quali=(select Quali from table where slNo='5'),
Exp=(select Exp from table where slNo='5')
where slNo='3';
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?
Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE t1.slNo = 3

Updating a price MYSQL Query

new here and new to MYSQL
I trying to create the following query and update all values from
["1"]
to
["2"]
My logic:
Table = mt_item
Column = price
Find all ["1"] values from merchant_id=55 and update to ["1"]
SELECT
price
FROM
mt_item
WHERE
price = '["1"]' AND
merchant_id = 55
UPDATE price
SET
price = `["2"]`
This works for me great, but now can i update the price to ["2"]
Hopefully you guys can help thanks.
You don't put UPDATE inside a SELECT query. You just use a WHERE clause in UPDATE
UPDATE mt_item
SET price = '["2"]'
WHERE price = '["1"]' AND merchant_id = 55
Simply use the following
Update mt_item
set price ='["2"]' where
price ='["1"]' AND merchat_id = 55
Also, if you want to use Select , you can do it like this
Update mt_item
set price ='["2"]' where merchat_id IN (SELECT merchant_id from mt_item where price='["1"]' and merchant_id = 55)

How do I update multiple columns in a table with multiple conditions in MySQL?

Is it possible to run an update query on multiple columns with multiple conditions in MySQL?
id name value price instock pp_flag
1 xyz 23 27 1 9
2 abc 28 12 0 8
For example above is the structure of a table myTable, where I want to run a query like:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12
where pp_flag = 8
Just wondering if I can do this in the same query in MYSQL.
Thanks
Use and in where clause:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12 and pp_flag = 8
What I understand is that you would like to do this
UPDATE TABLE myTable set value = 25 where id = 1
and
UPDATE TABLE myTable set price = 12 where pp_flag = 8
in a single statement.
You cannot do this as these are two independent WHERE-conditions.
You can use multiple condition to update column of a table .
As per your requirement you can use below query:
UPDATE TABLE myTable set value = 25
where id = 1 or (price = 12 and pp_flag = 8);
Hope it will help you!
Yes, it is possible by using the inbuilt IF function in MySQL (https://dev.mysql.com/doc/refman/8.0/en/if.html).
Query for your example would be:
UPDATE myTable SET
value = if(id=1, 25, value),
price = if(pp_flag=8, 12, price)

Updating DB depending on value

I have a table of objects:
object_id -- active -- tarif_id
1 1 5
2 0 6
3 1 19
And I have a table of tarifs:
tarif_id -- expire_date
5 2014-01-01
6 2014-05-01
19 2015-12-11
Is it possible to write an SQL that will check all object's tarif_id's expire dates with the Date.NOW and if Date.Now is more, then set object's active status to 0?
try this
UPDATE tblObject
INNER JOIN tbltarifs ON tblObject.tarif_id = tbltarifs.tarif_id
SET tblObject.active = 0
WHERE DATE_FORMAT(NOW(),'%m-%d-%Y') > DATE_FORMAT(expire_date,'%m-%d-%Y')
Sure you can. You're looking for the UPDATE function.
UPDATE ObjectsTable
SET active = 0
WHERE [tarif_id] IN (
SELECT [tarif_id]
FROM TarifTable
WHERE [expiredate] < DATE.Now --or whatever you are using to get the current date
)

updating the column of a table with different values

I have a table called person there are about 3 million rows
I have added a column called company_type_id whose default value is 0
now i want to update the value of company_type_id to 1
where person_id from 1 to 212465
and value of company_type_id to 8 where person_id from 256465 to 656464
how can i do this
I am using mysql
You can do that in one update statement:
update person
set company_type_id = 1
where
(person_id >= 1 and person_id <= 212465) or
(company_type_id = 8 and person_id >= 256465 and person_id <= 656464)
update person set company_type_id=1 where person_id>=1 and person_id<=212465;
I am sure you'll make the second update query by yourself.
Two SQLs:
1:
UPDATE person
SET company_type_id = 1
WHERE person_id BETWEEN 1 AND 212465
2:
UPDATE person
SET company_type_id = 8
WHERE person_id BETWEEN 256465 AND 656464