Updating a price MYSQL Query - mysql

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)

Related

SQL: update value based on following rows data

I have a table customer_service_types in phpmyadmin with these fields:
id (int) | customer_id (int) | service_type_int (int) | price (decimal) | created (datetime) | card (tinyint)
Here are some entries:
The application when the user adds a new service for a customer with a custom created date, is creating another service with id : 3 (payment) with today's date. I want to update all payments with the date of the following services.
So what I want to do is update the created field of the payments with the created value of the following services for specific client. So for example I need to update created of id: 168 with the value of created for the same customer_id but only of the following row if it has service_type_id != 3.
Tried so far:
UPDATE customer_service_types cst
SET cst.created = (SELECT created
FROM customer_service_types cst2
WHERE cst.id = (cst2.id - 1) AND cst2.service_type_id <> 3
)
WHERE cst.service_type_id = 3;
But I get this error:
You can't specify target table 'cst' for update in FROM clause
And I don't know if this query will produce the desired result
You can transfer your subquery to a Derived Table form instead. Try the following:
UPDATE customer_service_types AS cst
JOIN
(
SELECT id, created
FROM customer_service_types
WHERE service_type_id <> 3
) AS cst2
ON cst.id = (cst2.id - 1)
SET cst.created = cst2.created
WHERE cst.service_type_id = 3;
You can use a join:
UPDATE customer_service_types cst JOIN
customer_service_types cstnext
ON cstnext.id = cst.id and cstnext.service_type_id <> 3
SET cst.created = cstnext.created
WHERE cst.service_type_id = 3;

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)

Change default category in phmyadmin after update products in prestashop

I have a simple issue.
For a real programmer it schould be peanuts..
I want to change the category of a product when is this product is affected by a specific price rule.
this is what I have, but nothing happens ...
UPDATE psc5_product AND psc5_product_shop SET id_category_default=771 WHERE id_product IN (SELECT id_product FROM psc5_specific_price WHERE id_specific_price>0);
Thanks in advance!
Try this:
UPDATE `psc5_product`
SET `id_category_default` = 771
WHERE `id_product` IN (SELECT `id_product` FROM `psc5_specific_price` GROUP BY `id_product`);
UPDATE `psc5_product_shop`
SET `id_category_default` = 771
WHERE `id_product` IN (SELECT `id_product` FROM `psc5_specific_price` GROUP BY `id_product`);
Okay, this is a little more complicated as I thought.
When a preset specific price rule is starting (from - to - date) this is no update happening in the table psc5_product. So the trigger is not starting his work.
So I need mysql script that starts to work on the from date untill the end date, when the end date is over the change need to be turned back.
Example:
rule 1:
id_product 1 id_category_default 1 price rule from to 09-09-2018 00:00:00 to 10-09-2018 23:59:59 ---> needs to get a change to id_category_default 2
rule 2:
On 11-09-2018 00:00:00 id_product is no longer affected by the price rule and the id_category_default needs to get 1 again.
For the moment I don't have a clue how to start this...
The hard part for me, is the fact that the the category id in rule 2 (after the end of the promotion) is variable (I have 771 categories...) in rule 1 this is fixed, no so hard...
Try this :
//Deleting old products in this category
Db::getInstance()->executes('DELETE FROM '._DB_PREFIX_.'category_product WHERE id_category = 771');
//Recovery of products with a reduction
$req = Db::getInstance()->executes('SELECT id_product FROM '._DB_PREFIX_.'specific_price');
//Product update with the correct default category + Product attribution in the category
foreach ($req as $resu) {
$prod = new Product($resu['id_product']);
$prod->id_category_default = 771;
$prod->save();
$product->addToCategories(array(0 => 771));
}

MySql update field value based on other field values

Im trying to create a query that will do the following:
Here is my table:
ID total paid own status
------------------------------------------------
1 100.00 100.00 100.00 0
and here is the query:
$total = 50;
UPDATE cart table SET paid = $total, status = CASE WHEN $total >= own THEN 1 ELSE 2 END;
The idea is if total amount of field "total" is equal or greater than amount in field "own" update field "status" to 1, otherwize to 2.
Im not sure if I can do this with only one query, or I will need to update cart table, pull the results, perform calculations and than update again.
Try this:
UPDATE cart table SET paid = $total, status = If($total >= own, 1, 2)
Where ID = 1;
Its called conditional update query.
Your query will do what you mentioned. Except table keyword you defined it should not be there otherwise your query is fine:
UPDATE cart
SET paid = $total,
status = CASE WHEN $total >= own THEN 1 ELSE 2 END
WHERE
--your condition
A bit of a hack and SQL version dependent but what about:
UPDATE cart SET paid=$total, status=-($total>=own)+2;

how to increase or decrease the quantity in a field in my sql when we enter the new quantity

I'm doing a project I'm encountering a problem in my project.........
the problem is i would like to increase or decrease the quantity in my sql quantity field when i pass the issued or delivered quantity i.e....
initially the quantity in the quantity filed is 1 when i intake the product of a quantity 10 then automatically it should update the quantity as 10+1=11
so it must update as 11, if i remove a 1 quantity it should update as 0........
how to write a code in jsp..........
pls do help
if you want to INCREMENT the value of product, use the query below
UPDATE tableName SET columnName = columnName + toadd
and DECREMENT
UPDATE tableName SET columnName = columnName - tosubtract
so to apply in your case,
UPDATE tableName SET quantity = quantity + 10
or
UPDATE tableName SET quantity = quantity - 1