How to create a trigger on table to update field after one hour if a specific value have been set - mysql

My database stores the parking status information 1
The status field has three values (0 for empty, 1 for occupied and 2 for waiting)
If the parking status remains 2 for 1 hour I want to set it to 0 and set the username field to null.
Is there any way to do it with triggers.

You probably looking to create a Event like
CREATE EVENT myevent
ON SCHEDULE EVERY HOUR
DO
UPDATE mytable
SET status = 0, username = null
where status = 2;

Related

Update at the same time

When 2 client tries to update the same row at the same time only one is submitted and the other one is rejected so i want to create a queue to update one by one and sorry for bad English
I’m student I don’t know how to do is there any other way
I created java system for inventory i have store when 2 client tries to buy the same product at The same time , it must update the row for example product_quantity =10 If client 1 and client 2 tries to buy at the same time client 1 buy 2 it must be 10-2=8 and client 2 buy 3 it must 8-3=5 but the result appears 8 or 7 it mean accept only one this my code (select product_quantity from store where store_id = barcode)
If quantity > 0 { ( update store set products_quantity = ( sumquantity - quantitytobuy) where storeid = barcode
}

How to use auto increment to save log

I want to make a table to save the user log ( for example : login record)
Can the table check the id when using the auto increment ?
Example :
userid times login_record
1 1 xxxxx
2 1 xxxyy
1 2 xxyyy
1 3 xyyyy
2 2 xxxxx
not the way you mean, no. Mysql will not automatically look up the last row in the logins table with a matching user_id and increment the times field.
the mysql auto-increment is for the internally generated row id, not for column values.
If you insert one row for every login, you can get the user logins count by querying the table, select count(*) from logins where user_id = 1. If you add an index on user_id, this query will run pretty quick.

How to increase the value of a column by 1, every 2 rows in MySQL?

I need to make a database which has two columns: the first being Team ID, the second one being Member ID. The second column is an auto increment.
How do I make it so that the Team ID is 1 for Member ID 1 and 2, but increases to 2 for Member ID 3 and 4, and so forth?
Use a trigger to update team_id to member_id mod 2 + 1:
CREATE trigger team_id_generator after insert on tbl
for each row set team_id = member_id mod 2 + 1;
That will schedule execution every time a row is inserted. You will need to make sure member_id is defined.
Two options. The first is to change the increment to 1 after the first row is input and then insert the remaining rows. The other is to update the entire table after completion and adjust the auto increment by -1.
UPDATE table SET memberID = teamID - 1 WHERE teamID > 1

Prevent table from being updated while updating und deleting rows

I'm bulding a matchmaking system for a little game. My table which saves all players searching for opponents looks like this:
lobby
----------
userID
points
range
opponentID
Every 5 seconds the script tries to find an opponent within a certain range of points.
UPDATE lobby SET
opponentID = {myUserID}
WHERE points >= {myPoints} - {myRange} AND points <= {myPoints} + {myRange}
AND opponentID = 0 AND userID != {myUserID}
ORDER BY ABS(points - {myPoints}) ASC
LIMIT 1
If there are affected rows I remove the current player from the lobby and get the opponents ID:
DELETE FROM lobby
WHERE userID = {myUserID}
SELECT userID FROM lobby
WHERE opponentID = {myUserID}
Now my fear is that someone assigns the current user between the UPDATE and DELETE. How can I prevent this? Can I combine the UPDATE and DELETE?

How can I find the correct prior status row in this table with a SQL query?

Imagine a workflow for data entry. Some forms come in, they are typed into a system, reviewed, and hopefully approved. However, they can be rejected by a manager and will have to be entered again.
So, an ideal workflow would go like this:
recieved > entered > approved
But this COULD happen:
received > entered > rejected > entered > rejected > approved
At each stage, we record who updated the form to its current status - who entered it, who rejected it, or who approved it. So the forms status table looks like this:
form_id status updated_by updated_at
1 received Bob (timestamp)
1 entered Bob (timestamp)
1 approved Susan (timestamp)
2 received Bob (timestamp)
2 entered Bob (timestamp)
2 rejected Susan (timestamp)
2 entered Carla (timestamp)
2 rejected Susan (timestamp)
2 entered Sam (timestamp)
2 approved Susan (timestamp)
Here's what I'm trying to do: write a rejection report. I want a row for each rejection, and joined to that row, I want to see who did the work that got rejected.
As a human, I can see that, for a given status row with status 'rejected', the row that will tell me who did the faulty work will be the one that
shares the same form_id and
has a prior timestamp closest to the rejection.
But I'm having trouble telling MySQL that.
Can anybody see how to construct this query?
A subselect ended up working for me.
SELECT
`s1`.`form_id`,
(
SELECT
`s2`.`updated_by`
FROM
statuses s2
WHERE
`s2`.`form_id` = `s1`.`form_id`
AND
`s2`.`updated_at` < `s1`.`updated_at`
ORDER BY
`s2`.`updated_at` DESC
LIMIT 1
) AS 'made_rejected_change'
FROM
statuses s1
WHERE
`s1`.`status` = 'rejected'
Another solution that uses subselect (this time not a correlated subquery):
SELECT
w1.*,
w2.entered_by
FROM (
SELECT
wr.form_id,
wr.updated_at AS rejected_at,
wr.updated_by AS rejected_by,
MAX(we.updated_at) AS entered at
FROM workflow wr
INNER JOIN workflow we ON we.status = 'entered'
AND wr.form_id = we.form_id
AND wr.updated_at > we.updated_at
WHERE wr.status = 'rejected'
GROUP BY
wr.form_id,
wr.updated_at,
wr.updated_by
) w1
INNER JOIN workflow w2 ON w1.form_id = w2.form_id
AND w1.entered_at = w2.updated_at
The subselect lists all the rejecters and the immediately preceding entered timestamps. Then the table is joined once again to extract the names corresponding to the entered_at timestamps.
You want to get the rejected timestamp and then figure out the entry that appeared right before it based on the timestamp. I'm assuming that timestamp actually holds a date/time and isn't an SQL server timestamp field (completely different).
declare #rejectedTimestamp timestamp
select #rejectedTimestamp = timestamp
from table
where status = 'rejected'
select top 1 *
from table
where timestamp < #rejectedtimestamp
order by timestamp desc