inserting into one field value with different conditions using mysql - 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.

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;

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

How to query record based on timestamp and boolean value?

I have a payment table where I am storing payment related data.
Student_id is_payment_done timestamp
A111A11 false 2016/11/23 00:00:00
A111A11 false 2016/11/24 12:12:00
A111A11 false 2016/11/24 16:20:11*
B234A12 false
B234A12 false
B124A12 true
B123A12 false
Here I have student_id foreign key. Now I want to select the record against student_id where is_payment_done=false and I want to display the latest record for eg. in above table A111A11 record have all is_payment_dont=false and latest record is with timestamp as 2016/11/24 16:20:11*
I am very novice with SQL, I have tried using group by student_id but I don't know which aggregate function to use and how to check boolean fields of all the records after grouping.
My SQL method, there is no need for the sub query or anything because if all the records are false then the MAX(timestamp) is also false. If you didn't limit all records you could still use conditional aggregation to find the MAX(false timestamp).
SELECT
Student_id
,MAX(timestamp) as MaxTimeStamp
FROM
#Payment
GROUP BY
Student_id
HAVING
COUNT(*) = COUNT(CASE WHEN is_payment_done = 'false' THEN 1 END)
Note however in your example data A111A11, B123A12, and B234A12 all have only records where is_payment_done = 'false'
SELECT *
FROM Payment
WHERE Student_id = 'A111A11' AND
is_payment_done = 'false'
ORDER BY timestamp DESC
LIMIT 1;

Query issue selecting result from 1 table to another

I am not much of a code person, I am trying to run a query every 5 minutes in mssql.
Select user from Db1.dbo.tableA where requirement = 1
update Db2.dbo.tableB SET point = point + 5 where user = user;
I want to get user from Database 1 Table A where the requirement is 1 in DB, then update that user in Db2 Table B to match the user and increase current points by 5.
I am really newbie at this.
You can use UPDATE FROM with JOIN:
UPDATE b
SET b.point = b.point + 5
FROM tableB b
INNER JOIN tableA a
ON a.[user] = b.[user]
WHERE
a.requirement = 1
DECLARE #user <same datatype as in column>
Select #user = user from Db1.dbo.tableA where requirement = 1
update Db2.dbo.tableB SET point = point + 5 where user = #user;
Edit This will work only if there will be a single user as a result of the first query. For multiple users, the answer by Felix is more suitable
WITH tableAuser
AS
(
SELECT user FROM Db1.dbo.tableA WHERE requirement = 1
)
UPDATE b
SET b.point = b.point + 5
FROM Db2.dbo.tableB b
JOIN tableAuser a ON b.user = a.user

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;