MySql update field value based on other field values - mysql

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;

Related

Update only the greatest row - Sequential Based Update

I want to write a query to find the LOWEST SEQUENCE for status = REQUEST_PENDING then update it after a certain condition.
For example:
person_name
status
sequence
a
request_progressed
1
b
request_pending
2
c
request_pending
3
If the person named A is done, it's status would change from "request_progressed" to "request_done"; then the next person B will have his status changed to "request_progressed" while the person C stays the same because its sequence is the 3rd.
So I just need to know how do I change the person B status by finding the lowest sequence which has "request_pending" status and THERE IS (SUPPOSED TO BE) ONLY ONE "request_progressed" on the table that is why when it is updated to "request_done" the next "request_pending" need to be updated to "request_progressed".
I tried the following:
update table
set status = 'request_progressed'
where status = 'pending' AND sequence = min(sequence)
In MySQL - you can specify order by and limit in update queries:
update t
set status = 'request_progressed'
where status = 'request_pending'
order by sequence
limit 1
In PostgreSQL - you still need order by and limit but they can only be used inside a subquery:
update t
set status = 'request_progressed'
where (status, sequence) in (
select status, sequence
from t
where status = 'request_pending'
order by sequence
offset 0 rows fetch first 1 row only
)

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)

Update max value from a mysql table

I'm trying to update a table's value using max(col) in a subquery, but for some reason, it's updating all values that match the user_id column I'm using.
This is my table structure:
user_id | steps | in_date
--------+-------+-----------
8 |10 | 1522246892
8 |10 | 1522250713
7 |10 | 1522250799
And this is my query:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = (SELECT max(in_date) WHERE user_id = 8);
I expected it to update only the second column, but it instead updates both columns with user_id = 8. Why isn't it working as expected? What am I doing wrong?
Edit: Thanks to Manoj's comment, I changed the query to the following, and it works:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
ORDER BY in_date DESC LIMIT 1;
Doing it his way is even better, since I don't have to run two queries, and already gets the highest one by id.
You will encounter sql error "You can't specify target table 'userdata' for update in FROM clause" when you use same table in the sub-query to update the same table.
Its strange that you say its running because,
- you missed from clause in your sub-query
- you can't use same table
Can you please be more specific.
seems you missed the from clause in subselect and for avoid conflit between select and updated value you could build a temp table using ainner subselect
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = ( select max_date from (
SELECT max(in_date) max_date FROM userdata WHERE user_id = 8) t);
and in this way the AND clause could be always true

inserting into one field value with different conditions using mysql

i have table visits
with columns like the below
visit_id
member_id
logout_datetime(format like this ...'yyyy-MM-dd HH:mm:ss')
visit_message (like accept, refuse)
i want to insert the logoutdatetime value into visits table
where member_id = 1 and visit_message = "accept"
how can i do that using mysql
would any one pls help on this ...
many thanks in advance..
What about:
UPDATE visits
SET logout_datetime = NOW()
WHERE member_id = 1 AND visit_message = "accept"
This is an UPDATE, not an INSERT, since the row already exists where member_id = 1 and visit_message = 'accept':
UPDATE visits SET logout_datetime = '2011-10-24 07:01:22' WHERE member_id = 1 AND visit_message = 'accept';
If you intend to use the current timestamp for logout_datetime, substitute the function NOW() for the literal date in my example.