I have some a MYSQL db with a DATETIME field that updates itself every minute while a user is logged in via AJAX. This works perfectly. However I am trying to get a count of how many logged in users there are and it is not working
$table = 'mytable';
$query = "SELECT * FROM $table WHERE lidtm > DATE_SUB(DATE(now()), INTERVAL 1 MINUTE)";
$result = mysqli_query($dbc, $query);
$num = mysqli_num_rows($result);
echo $num;
theoretically this should tell me how many users are logged in, because each users 'lidtm' field updates every minute while they are logged in, so if the DATE_SUB function counts the rows in which the lidtm field is > that now() - 1 minute, it should tell me all users that have been updated by my ajax within the last minute.
but for some reason it always returns all rows.
what am I missing. I need a count of how many rows the 'lidtm' field is within the last minute.
Wrapping now() with date() casts it to a date, i.e., without a time:
select date(now());
+-------------+
| date(now()) |
+-------------+
| 2017-05-16 |
+-------------
If you want to measure minutes, you need the time:
select now();
+---------------------+
| now() |
+---------------------+
| 2017-05-16 13:56:00 |
+---------------------+
So just take out the call to date(), and use:
select date_sub(now(), interval 1 minute);
+------------------------------------+
| date_sub(now(), interval 1 minute) |
+------------------------------------+
| 2017-05-16 13:56:17 |
+------------------------------------+
Related
I have this query that selects every appointment with status Pending. Works well. The problem with this query it will also select appointments that are pending in the past. I only want to display those that are either today at a later hour than current_time or simply at a later date. Time and date are in a different column. In the example below only the second and third row should be returned. I'm giving you the full query as it is used and working in my app right now. How can this be achieved?
user_schedule table
id | customer_id | date | time | cleaning_status
1 | 345 | 2020-06-09 | 08:00:00 | Pending
2 | 768 | 2020-06-09 | 19:00:00 | Pending
3 | 913 | 2020-06-11 | 07:00:00 | Pending
PHP
if(!empty($_POST)){
//variables
$current_time ='16:00:00';
$current_date ='2020-06-09';
$my_city ='Miami';
$sstatus_o = 'Pending';
//query
$data = $conn->prepare("select *,us.id as orderid,us.customer_id
as ownerId from user_schedule us
left join users u
on us.customer_id=u.id
LEFT JOIN user_avatar ua
ON us.customer_id=ua.user_id
and ua.last_update = (select
max(last_update)
from user_avatar ua1 where
ua.user_id=ua1.user_id)
left join user_address uad
on us.customer_id=uad.user_id
where (uad.city LIKE ?) AND
us.cleaning_status=? ORDER BY us.id DESC");
$data->bind_param('ss',$my_city,$sstatus_o);
$data->execute();
$result_data = $data->get_result();
}
You can add another constraint to your query that checks whether the timestamp formed from your date and time values is greater than your $current_date and $current_time values i.e.
WHERE uad.city LIKE ?
AND us.cleaning_status = ?
AND TIMESTAMP(us.date, us.time) > TIMESTAMP(?, ?)
and then add the $current_date and $current_time variables to the bind_param i.e.
$data->bind_param('ssss', $my_city, $sstatus_o, $current_date, $current_time);
I have a query that gets executed based on the date range the user chooses. For example: 12-12-2019 to 1-13-2020.
// Retrieve count of attendance, no shows and cancellations per user selected category and sort by week number
$q = "SELECT YEARWEEK(`start_time`, 0) AS weekno,
SUM(`is_no_show` = 0 AND `is_cancelled` = 0) as attended,
SUM(`is_no_show` = 1) AS no_shows,
SUM(`is_cancelled` = 1)AS cancelled
FROM `myTable`
WHERE (`start_time` > :start_date AND `start_time` < :end_date)
AND category LIKE :cohort_type
GROUP BY weekno";
My issue is that this query stops pulling in data after 12-23-2019. It seems to stop at the last week of the year and not go into 2020 as week 1. How do I account for this? Any suggestions or tips is greatly appreciated!
Thank you.
General DB Structure:
+------------+-----------+-----------+
| start_time | no_shows | cancelled |
+------------+-----------+-----------+
| 2019-12-20 | 1 | 0 |
| 2019-12-21 | 0 | 0 |
| 2019-12-22 | 0 | 1 |
GOAL: I want to SUM the data on a weekly basis
EDIT: YEARWEEK() skips the first week of 2020 and goes straight to week 2.
Your query is missing a GROUP BY criteria on the year; as of now, it will mix weeks belonging to different years, which I assume is not what you want. I would suggest using YEARWEEK(), which takes the year in account.
Also, your ilter on the date seems akward, as it is mixing parameters and string concatenation; you can use half-open intervals instead (and proper parameter bindings).
Consider:
SELECT
YEARWEEK(`start_time`) AS weekno,
SUM(`is_no_show` = 0 AND `is_cancelled` = 0) attended,
SUM(`is_no_show` = 1) no_shows,
SUM(`is_cancelled` = 1) cancelled
FROM `attendee_categories_appts_joined`
WHERE
`start_time` > :start_date
AND `start_time` < :end_date
AND category LIKE :cohort_type
GROUP BY weekno
Database table i have:
S.no | j_id |age | e_date |
-------------------------------------
1 | 1 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 1 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 2 |32 | 2018-05-09 |
-------------------------------------
-------------------------------------
1 | 2 |32 | 2018-04-16 |
-------------------------------------
-------------------------------------
1 | 1 |32 | 2018-09-16 |
-------------------------------------
-------------------------------------
1 | 3 |32 | 2018-04-16 |
------------------------------------
In my table I have expiry date I want to get the count of the result whose expiry date (90 days before) is equal to current date.
like I have expiry date 2018-05-09 and current date is 2018-02-2018 (90 days before date ) now i want to get to the count of the 90 days before result by query.
select * from yourtable
where datediff(CURDATE(), e_date) > 90
try to use datediff to get the different date count in day. Hope that this is what you want to get.
I like to keep the logic on the PHP side as much as possible, so I would probably calculate my expiry date in PHP and just add a simple where to the query. In Laravel that could look something like this:
$expireTreshold = Carbon::now()->addDays(90);
$expireCount = $myModel->where('e_date', '<=', $expireTreshold)->count();
For getting the current date, you can use CURDATE(), CURRENT_DATE(), and NOW() any one of these functions would get the current date. While the DATEDIFF() will get the difference between two periods (start date to end date).
If you only need to get the expiry dates that fit the 90 days condition use this :
SELECT e_date
FROM tableName
WHERE
e_date >= NOW()
AND datediff(e_date, NOW()) <= 90
In the query, you're scanning for future dates (from the current date and forward) and then get the differences, if the differences is less than or equal to 90 days, then it'll be selected.
if you need to show how many days left to each user:
SELECT sno, datediff(e_date, NOW())
FROM test
WHERE
e_date >= NOW()
AND datediff(e_date, NOW()) <= 90
It will work fine.
SELECT count(*) FROM table WHERE e_date < CURDATE() - INTERVAL 90 DAY;
OR
SELECT count(*) FROM table WHERE e_date < NOW() - INTERVAL 90 DAY;
cheers :)
Try this:
$your_date = "2018-01-01";
query = 'SELECT COUNT(*)
FROM thetable WHERE e_date =
DATE_ADD($your_date, INTERVAL 90 DAY)';
I've been scratching my head of the best way to do this, I run a small forum and want to change the ordering of the posts.
ID | Subject | lastpost
____________________
1 | Test | 2014-06-2012 00:00:00
2 | Test | 2014-06-2012 00:00:00
3 | Test | 2014-06-2012 00:00:00
4 | Test | 2014-06-2012 00:00:00
5 | Test | 2014-06-2012 00:00:00
6 | Test | 2014-06-2012 00:00:00
7 | Test | 2014-06-2012 00:00:00
Ideally I would like it to display results where lastpost < 60 mins THEN display the rest by the id desc
I have tried
SELECT * FROM `table` ORDER by `lastpost` > (time), `id` desc
but seem to be drawing a blank.
Thanks in advance.
You want:
SELECT *
FROM table
ORDER BY (`lastpost` > date_sub(now(), interval 60 minute)) DESC,
(CASE WHEN `lastpost` > date_sub(now(), interval 60 minute) THEN lastpost ELSE NULL END) DESC,
id;
The first condition puts the most recent posts first. The second sorts those by the post date (presumably the most recent first). The rest are sorted by id desc.
I assume that lastpost is of column type datetime and not a string - if not, you should change that.
So
SELECT * FROM table WHERE lastpost > NOW() - INTERVAL 1 HOUR ORDER BY id DESC
should solve your problem.
I have in my database:
id | text | date
1 | sdsd | 2012-01-23 08:11:00
2 | asd | 2012-01-23 08:24:00
3 | dfdf | 2012-01-23 08:34:00
4 | fdf | 2012-01-23 08:41:00
5 | xcvx | 2012-01-23 08:48:00
etc
on my server is cron with 10 minutes intervals,
for example:
08:03:00
08:13:00
08:23:00
08:33:00
08:43:00
08:53:00
how is the best method for get this values from databases with SQL for PHP?
This query will return all rows that have date > last 10 minutes:
SELECT *
FROM `table`
WHERE `date` > CURRENT_TIMESTAMP - INTERVAL 10 MINUTE
It is assumed that your table does not contain any future dates.
Do a SQL query which looks for a time in the date field that is plus or minus 4 minutes of the current time according to PHP.
In your cron script you could do something like this:
$res = mysql_query('select * from table where `date` < now()');
Then you simply do the thing you want to do with those rows.