Sum the time user spent in particular place PHP/MYSQL - php

My table columns are :
--------------------------------------------------------------------
| EVENT_ID | EVENT_WORKDAY | EVENT_PLACE | EVENT_TIME | EVENT_TYPE |
--------------------------------------------------------------------
Basically, it's a log of what a person did at work. What I want to do is to know how much time the person spent in a particular place.
EVENT_WORKDAY is what will be pased into WHERE argument (I already know at which workdays he was in that place)
EVENT_PLACE is an id of a place (it might be 1, 2 or 3)
EVENT_TIME is a datetime field
EVENT_TYPE is a type of event (1 - at some place, 2 - having a break, 3 - outside)
For example:
A person has started logging at 6:00 being outside
(EVENT_TYPE=3, EVENT_PLACE=null). Then at 6:15 moved to a place (EVENT_PLACE=1, EVENT_TYPE=1), he continued to log it every 15 minutes till 9:53, when he went for a break (EVENT_TYPE=2, EVENT_PLACE=null), at 10:22 he changed the place EVENT_TYPE=1, EVENT_PLACE=2
What I want to get is the time spent at place 1. It must be the time between first event and event at 9:53. So the output must be, that he was in place 1 for 3 hours 38 minutes (or its equivalent in minutes/seconds etc.)
-----------------------------------------------------------------------------
| EVENT_ID | EVENT_WORKDAY | EVENT_PLACE | EVENT_TIME | EVENT_TYPE |
-----------------------------------------------------------------------------
1 | 1 | null | 2016-04-02 06:00:00 | 3
2 | 1 | 1 | 2016-04-02 06:15:00 | 1
3 | 1 | 1 | 2016-04-02 06:30:00 | 1
4 | 1 | 1 | 2016-04-02 06:45:00 | 1
... ...
15 | 1 | 1 | 2016-04-02 09:45:00 | 1
16 | 1 | null | 2016-04-02 09:53:00 | 2
17 | 1 | 2 | 2016-04-02 10:22:00 | 1

Related

MySQL dynamically find available reservations

I have a requirement to create a room reservation system with dynamic booking slots (a reservation can start at any time and last anywhere from 15 minutes up to several days), but I can't crack the logic.
Reservations need to consider the opening times of the rooms (some rooms are based at sites that are available for 24 hours and others only a few hours per day. Some days, rooms may be entirely unavailable or have reduced opening hours). A reservation cannot jump across opening times with a gap greater than 1 second. For example, it is ok for a reservation to be accepted if the closing time on Sunday is 23:59:59 and it reopens at 00:00:00 on Monday, as this isn't considered a gap. However, if the opening time were 01:00:00 Monday, this would not be acceptable.
In addition, the database will have rooms across the world, operating in different time zones, so this will also need to be a consideration in the final design. Finally, a reservation cannot begin or end within 1 hour of another reservation.
Here is a stripped back schema of our current tables:
site
+---------------------------------------+-------+------------------------+---------------------------------------+
| uuid | id | name | address |
+---------------------------------------+-------+------------------------+---------------------------------------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6f3 | 1 | NAMCO Library | 6 London Rd, Los Angeles, USA, 087098 |
+---------------------------------------+-------+------------------------+---------------------------------------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6f6 | 2 | Livingston Hotel | 6 Jump Street, London, UK, E56JK |
+---------------------------------------+-------+------------------------+---------------------------------------+
opening_times
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| id | site_id | day_of_week | open | close | date_outlier | timezone |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 1 | 1 | 1 | 10:00:00 | 15:59:59 | NULL | America/Los_Angeles |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 2 | 1 | 2 | 07:00:00 | 21:59:59 | NULL | America/Los_Angeles |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 3 | 1 | 3 | 07:00:00 | 21:59:59 | NULL | America/Los_Angeles |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 4 | 1 | 4 | 07:00:00 | 21:59:59 | NULL | America/Los_Angeles |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 5 | 1 | 5 | 07:00:00 | 21:59:59 | NULL | America/Los_Angeles |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 6 | 1 | 6 | 07:00:00 | 21:59:59 | NULL | America/Los_Angeles |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 7 | 1 | 7 | 10:00:00 | 22:59:59 | NULL | America/Los_Angeles |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 8 | 2 | 1 | 00:00:00 | 23:59:59 | NULL | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 9 | 2 | 2 | 00:00:00 | 23:59:59 | NULL | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 10 | 2 | 3 | 00:00:00 | 23:59:59 | NULL | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 11 | 2 | 4 | 00:00:00 | 23:59:59 | NULL | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 12 | 2 | 5 | 00:00:00 | 23:59:59 | NULL | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 13 | 2 | 6 | 00:00:00 | 23:59:59 | NULL | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 14 | 2 | 7 | 00:00:00 | 23:59:59 | NULL | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 15 | 2 | NULL | 10:30:00 | 16:59:59 | 2022-05-04 | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
| 16 | 2 | NULL | NULL | NULL | 2022-05-05 | Europe/London |
+----+----------+--------------+-----------+-------------+---------------+-----------------------+
All times in this table are in local timezones.
Row 15 on this table shows a reduced opening time at the Livingston Hotel on May 4th, 2022.
Row 16 on this table shows that the Livingston Hotel does not allow any bookings on May 5th, 2022.
rooms
+---------------------------------------+-------+---------+------------------------+-------------+-----------+
| uuid | id | site_id | name | hourly_cost | currency |
+---------------------------------------+-------+---------+------------------------+-------------+-----------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6d3 | 1 | 1 | Blue Room | 60.00 | USD |
+---------------------------------------+-------+---------+------------------------+-------------+-----------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6d6 | 2 | 1 | Red Room | 35.50 | USD |
+---------------------------------------+-------+---------+------------------------+-------------+-----------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6d7 | 3 | 1 | Purple Room | 15.50 | USD |
+---------------------------------------+-------+---------+------------------------+-------------+-----------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6d8 | 4 | 2 | Dragon Room | 55.50 | GBP |
+---------------------------------------+-------+---------+------------------------+-------------+-----------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6d9 | 5 | 2 | Bus Room | 85.00 | GBP |
+---------------------------------------+-------+---------+------------------------+-------------+-----------+
reservations
+---------------------------------------+---------+-------------+----------------------+---------------------+--------------+
| uuid | room_id | customer_id | start | end | cancelled_at |
+---------------------------------------+---------+-------------+----------------------+---------------------+--------------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6h3 | 3 | 1 | 2022-05-04 14:01:53 | 2022-05-01 14:16:53 | NULL |
+---------------------------------------+---------+-------------+----------------------+---------------------+--------------+
| 418bbdcb-09c9-4407-adb4-162fb3e5e6h4 | 4 | 3 | 2022-05-03 11:01:53 | 2022-05-03 12:01:53 | NULL |
+---------------------------------------+---------+-------------+----------------------+---------------------+--------------+
Start and end times in this table are stored in UTC.
The SQL that we currently use does not correctly deal with reservations spanning different days and has no consideration of timezone conversions for opening times.
We're using prepared statements to replace the following strings in the SQL.
:start is the start time of the reservation in UTC
:end is the end time of the reservation in UTC
SELECT
sites.id site_id,
sites.site_ref,
sites.name site_name,
rooms.name room_name,
FROM
sites
JOIN
rooms
ON
rooms.site_id = sites.id
LEFT JOIN
reservations bookings
ON
rooms.id = reservations.room_id
AND (
(:start + INTERVAL 1 HOUR) <= reservations.end
AND
(:end - INTERVAL 1 HOUR) >= reservations.start
AND
reservations.cancelled_at IS NULL
)
JOIN
opening_times
ON
sites.id = opening_times.site_id
AND (
opening_times.site_id NOT IN (
SELECT
site_id
FROM
tbl_opening_times
WHERE
DATE(:start) = date_outlier AND open IS NULL AND close IS NULL
) OR (
(DATE(:start) = opening_times.date_outlier AND TIME(:start) >= opening_times.open AND TIME(:end) <= opening_times.close)
OR
(DAYOFWEEK(DATE(:start)) = opening_times.dow AND TIME(:start) >= opening_times.open AND TIME(:end) <= opening_times.close)
)
)
WHERE
reservations.uuid IS NULL
ORDER BY
sites.id
Tests
A query for a reservation lasting 3 hours starting at 2022-05-04 14:00:00 (UTC) should list room id 1,2 at NAMCO Library. The rooms at the Livingston Hotel should not be listed as the end of the reservation will happen after the particular closing time of the hotel.
A query for a reservation lasting two days starting at 2022-05-01 12:30:00 (UTC) should list room id five at Livingston Hotel.
A query for a reservation lasting 40 minutes starting at 2022-05-05 15:30:00 (UTC) should list room id 1,2,3 at NAMCO Library.
I'm open to solutions that involve processing on the application side (we use PHP) if the complexity is too much for MySQL alone.
FYI we're using MySQL 8.*

mysql. I want to get the max value of all rows with a specific date and use the corresponding fields

My table 'players_online'
id | count | date | time | day | max-players
0 | 55 | 26/05/16 | 13:00 | 5 | 300
1 | 33 | 26/05/16 | 13:10 | 5 | 300
2 | 43 | 26/05/16 | 13:20 | 5 | 300
3 | 100 | 27/05/16 | 13:00 | 6 | 300
4 | 43 | 27/05/16 | 13:10 | 6 | 300
5 | 56 | 27/05/16 | 13:20 | 6 | 300
desired output (todays highest count)
id | count | date | time | day | max-players
3 | 100 | 27/05/16 | 13:00 | 6 | 300
Also
(yesterdays highest count)
id | count | date | time | day | max-players
0 | 55 | 26/05/16 | 13:00 | 5 | 300
And
(Higest total count)
id | count | date | time | day | max-players
3 | 100 | 27/05/16 | 13:00 | 6 | 300
My knowledge of mysql has gotten rusty and I have no idea how to get it right. previously I tried the following which didn't select the right time.
SELECT MAX(count) AS maxcount, date, time FROM players_online WHERE date='".date('d-m-y')."'
I'm building a little stats page where you can view the highest player count of a minecraft server today (until now), yesterday and the highest player count of all time with their corresponding date, time, day and max-players.
Hope somebody can give me a hand.
You can just do a simple query that orders by count and is limited to 1.
SELECT * FROM players_online WHERE date = '27/05/2016' ORDER BY count DESC LIMIT 1
That should get you the highest count.

Private Message System [Inbox Query]

I have a table called "message" and I want to show all talk between users (last message)
Table structure:
message_id | user_id | recipient_id | message | status | date
Example rows:
1 | 1 | 2 | Hello | 0 | 2016-03-26 12:00:00
2 | 2 | 1 | Hi | 0 | 2016-03-26 12:05:00
3 | 1 | 3 | Are you there? I want meet you! :P | 0 | 2016-03-26 12:20:00
4 | 1 | 2 | How are you? | 0 | 2016-03-26 12:10:00
5 | 2 | 1 | Fine :) | 0 | 2016-03-26 12:15:00
6 | 5 | 1 | Hi :D | 0 | 2016-03-26 15:00:00
So, result should be (for user_id == 1):
3 | 1 | 3 | Are you there? I want meet you! :P | 0 | 2016-03-26 12:20:00
5 | 2 | 1 | Fine :) | 0 | 2016-03-26 12:15:00
6 | 5 | 1 | Hi :D | 0 | 2016-03-26 15:00:00
First you sort by date, then you group by user_id
SELECT *
FROM
(
SELECT * from messages
WHERE `user_id`=1 or `recipient_id`=1
ORDER BY `date` DESC
) m
GROUP BY `user_id`
Result:
3 | 1 | 3 | Are you there? I want meet you! :P | 0 | 2016-03-26 12:20:00
5 | 2 | 1 | Fine :) | 0 | 2016-03-26 12:15:00
6 | 5 | 1 | Hi :D | 0 | 2016-03-26 15:00:00
sqlFiddle demo
I hope I will explain better than in my comment, here it goes:
First, you need to have logging system, so when user logins you save his ID to session.
Second, based on user that is logged you show all messages in "inbox" that he has with other users,select * from message where user_id = $_SESSION['id'], you can join tables and show it names from user and recipient
Third when he clicks on some chat with other person then you show all messages between them ordered by date based on user id and recipient_id
Would a query like select * from messages group by user_id order by date sort you? Fiddle demo.

Cumulate values for each user

I have a MySQL table with the following structure:
| id | date | type | user | approved |
======================================================
| 1 | 2015-01-30 20:32:01 | 2 | 1 | 0 |
| 2 | 2015-01-31 19:40:12 | 1 | 2 | 1 |
| 3 | 2015-02-01 11:12:08 | 2 | 1 | 0 |
| 4 | 2015-02-01 11:32:13 | 4 | 3 | 1 |
| 5 | 2015-02-01 17:25:22 | 6 | 2 | 1 |
I now would like to cumulate the times a task was approved (1) for each user. This is what the result should be:
user 1 --> 0
user 2 --> 2
user 3 --> 1
Is there a way to accomplish this by a single SQL query or would I need to do this outside of MySQL in my php script?
you can just group and summarize values:
select user, sum(approved)
from table
group by user

How to summ total amount of equipments in given date ranges [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Calculating total quantity of equipments for a date range
Project: I am working on a project which is about some rooms and equipments using in the rooms. The software is about scheduling the equipments in the rooms. In other words, it is a reservation software that reserves selected equipments in separate rooms for needed dates and times ranges. I have many tables in MYsSQL database working with Php but I will mention the tables my question is about. The tables I will relate my questions are equipment table (Table A), schedule table (Table B) and equipments using in the related schedule (Table C).
Table A: equipment list table
+------+----------+-----------+
| eqid | eqName | available |
+------+----------+-----------+
| 1 | book | 90 |
| 2 | pen | 82 |
| 3 | computer | 25 |
+------+----------+-----------+
In table A; eqid represents unique id of an equipment, eqName represents name of an equipment, available represents total available equipments existing.
Table B: schedule table
+------------+------------+------------+-----------+----------+--------+
| scheduleid | startDate | endDate | startTime | endTime | office |
+------------+------------+------------+-----------+----------+--------+
| 1 | 2012-08-27 | 2012-08-27 | 08:30:00 | 10:00:00 | room1 |
| 2 | 2012-08-27 | 2012-08-27 | 09:30:00 | 11:00:00 | room3 |
| 3 | 2012-08-28 | 2012-08-30 | 08:30:00 | 12:00:00 | room2 |
| 4 | 2012-08-29 | 2012-08-31 | 11:30:00 | 14:00:00 | room1 |
| 5 | 2012-08-28 | 2012-08-28 | 10:30:00 | 14:00:00 | room3 |
| 6 | 2012-08-27 | 2012-08-30 | 08:30:00 | 10:00:00 | room4 |
| 7 | 2012-08-27 | 2012-08-27 | 10:30:00 | 12:00:00 | room4 |
| 8 | 2012-08-27 | 2012-08-30 | 08:30:00 | 11:00:00 | room6 |
| 9 | 2012-08-27 | 2012-08-27 | 10:30:00 | 12:00:00 | room5 |
+------------+------------+------------+-----------+----------+--------+
In table B; scheduleid represents unique id for a schedule, startDate and endDate are date range for a schedule, startTime and endTime time range for a schedule, office means that where the schedule will take place. Let me give an example here. Scheduleid 1 means there is a reservation on 27th of august 2012, Monday and it is from 08.30 to 10:00. As it start and end on same day this is just one day reservation in room1. However, Scheduleid 3 means there is a reservation starts on 28th of august 2012, Tuesday and goes on until 30th of august 2012, Thursday at 08:30-12:00... in other words, it lasts for 3 days and everyday from 08:30 to 12:00... So there is a reservation from Tuesday to Thursday at 08:30 to 12:00 in room2... I hope this is clear.
Table C: equipments using in the related schedule
+--------+------------+------+-------------+
| Autoid | scheduleid | eqid | amountInSch |
+--------+------------+------+-------------+
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 3 |
| 3 | 1 | 3 | 1 |
| 4 | 2 | 1 | 1 |
| 5 | 2 | 2 | 1 |
| 6 | 2 | 3 | 2 |
| 7 | 3 | 2 | 1 |
| 8 | 3 | 3 | 3 |
| 9 | 4 | 2 | 1 |
| 10 | 4 | 3 | 1 |
| 11 | 5 | 1 | 1 |
| 12 | 6 | 1 | 1 |
| 13 | 6 | 3 | 2 |
| 14 | 6 | 2 | 4 |
| 15 | 7 | 1 | 5 |
| 16 | 7 | 2 | 6 |
| 17 | 8 | 2 | 1 |
| 18 | 9 | 1 | 8 |
| 19 | 9 | 2 | 5 |
| 20 | 9 | 3 | 6 |
+--------+------------+------+-------------+
In table C: Autoid represents unique automatic id generated by auto-increment, scheduleid comes from Table B, eqid comes from Table A, amountInSch represents how many (amount) equipment will use in the related schedule. I want to give an example here. Scheduleid 1 in Table C, there are 3 rows. This means that scheduleid 1 related in TAble B will use 2 books (eqid 1), 3 pens (eqid 2) and 1 computer (eqid 3) in room1 specified dates and times in table B . Another example is that scheduleid 3 in Table C is related 2 rows. It means that 1 pen (eqId 2) and 3 computers (eqId 3) will be using in room2 from 27th to 30th of august 2012 everyday from 08:30 to 12:00.
The above is the explanation and give some information about the project. The table rows are not permanent. When you make a reservation, there will be a new row in Table B and if it is selected an equipment, there will be new rows in table C...
The Question:
I want to calculate left amount of a specific equipment when I supply eqId, startDate, endDate, startTime and endTime...
An example:
eqId: 1 (book)
startDate: 2012-08-27
endDate: 2012-08-27
startTime: 08:30:00
endTime: 12:00:00
Result should be: 14 books used in schedule and 76 left available books
Because: if you look scheduleIds and related eqIds, you will only see 1, 2, 6, 7, 9 scheduleIds related to my query(dates and eqId). If you sum the all amount of related in Table C, you will get the wrong result. In other words, related amounts for eqId(1-book) and for 1, 2, 6, 7, 9 scheduleIds are 2, 1, 1, 5, 8 respectively. So if you sum them you will get 17 which is wrong. Because, 1 and 9 schedule don't intersect each other in terms of start and end Times, and 6 and 7 don't intersect each other either. as a result of them 2 stays lonely and can be count separately. We must consider 1 and 9 as summed 8 because 8 is bigger than 2. it is same for 6 and 7, considered as 5 because of 5 is bigger than 1...
So folks! I am not sure how I can sum/ this in programming algorithm. Is there a way to do in SQL or do I have to use PHP and Mysql together? and How?
SQLFiddle Records
There are different ways of achieving this - I will point you at something that can be used with most DB-engines.
In mysql, it is possible to merge tables in a single query. What you need is something along the line of this:
$eqid = 1;
$startDate = "2012-08-27";
$endDate = "2012-08-27";
$startTime = "08:30:00";
$endTime = "12:00:00";
$sql = "SELECT SUM(`amountInSch`) FROM `table_c`,`table_b` WHERE `eqid` = $eqid
AND `startDate` >= '$startDate' AND `startDate` <= '$endDate' AND `endDate`
<= '$endDate' AND `endDate` >= '$startDate' AND `startTime` >= '$startTime'
AND `startTime` <= '$endTime' AND `endTime` <= '$endTime' AND `endTime` >=
'$startTime' AND `table_b`.`scheduleid` = `table_c`.`scheduleid`";
$r = mysql_query($sql);
$n = mysql_result($r,0,0);
The trick is to use the table_b`.`scheduleid` =
`table_c`.`scheduleid (some quotes are missing due to markup here) to merge the two tables.

Categories