Using a datetime margin in a SQL Query - php

Bit of a double barrel question here. I have a table with 4 columns:
id | userid | systemid | datetime
Every time someone logs in a record is added to this table. What I'm trying to achieve is a query that allows me to count the number of UNIQUE userid's that are logged where system id = x from between 1st of the current month until the current time.
I'm hovering with the current syntax but am not aware of the best way to only add to the count once for each userid:
$mysqli->query("select count(userid) as c from log_logins where datetime between '[1st of this month]' and curdate()");
$result = $mysqli->fetch_object()->c;
Also, for this to work does the datetime column have to be a unix timestamp? The datetime column is currently set as this value: <?php date('d/m/Y H:i', time()); ?>
Thanks for any help in advance!
Michael

Use COUNT(DISTINCT userid):
$mysqli->query('
SELECT COUNT(DISTINCT userid) AS c
FROM log_logins
WHERE systemid = x
AND datetime BETWEEN \'[1st of this month]\' AND CURDATE()
');
As regards the format of the datetime column, it should be a MySQL TIMESTAMP type (which is stored internally as a UNIX timestamp).

Related

PHP MySQL select rows where date time value within the last n minutes

I have a MySQL table that looks a little like this:
Timestamp | Content
-----------------------------------------
2018-10-28 12:59:47 | "Some string"
2018-10-29 04:13:48 | "Some other string"
The Timestamp column is of the datetime type. Values are being created for insertion into the column using the PHP date() function. For example, date("Y-m-d H:i:s")
I'd like to use PHP to select all rows where the timestamp value is within the last n minutes.
I know how to select all rows where the date is a particular value. For example, the following will select all rows where the timestamp matches the current date/time:
mysqli_query($link, "SELECT * FROM tablename WHERE Timestamp = '" . date("Y-m-d H:i:s") . "'");
How would I make a query to select all rows where the Timestamp value is within the last, say, 49 minutes ?
Use this query (There's no need to involve PHP date functions, when MySQL has them built in. See DATE_SUB() for more info):
SELECT *
FROM tablename
WHERE Timestamp > DATE_SUB(NOW(), INTERVAL 49 MINUTE)

MySQL filter for specific dates when dates just saved as UNIX timestamp

I am using PHP to access a MySQL database. I have a table built up like this:
Table headers:
id (INT, auto increment), profileid, timestamp
Table content:
1, 12345678, 1513814399 (= 12/21/2017)
2, 13451983, 1513814400 (= 12/21/2017)
3, 12345678, 1513944000 (= 12/22/2017)
4, 12345678, 1513944001 (= 12/22/2017)
The table shows which profileids have been called by a website visitor at which time.
So my question is now, how is it possible to show for example:
"Give me the number of entries for profile no. 12345678 called on 12/22/2017", which would be "2" in this case.
I tried it with this query:
SELECT COUNT(profileid), from_unixtime(timestamp, '%d') AS day, from_unixtime(timestamp, '%m') as month, from_unixtime(timestamp, '%Y') as year WHERE profileid='12345678' AND day=22 AND month=12 AND year=2017;
But it is not possible to access the columns "day", "month" and "year" because they to not exist in the table.
Can someone give me a tip how to do this? Another way would be to create three new columns (timestamp_day, timestamp_month and timestamp_year), but that's not a nice solution.
Thank you in advance!
teha
Just produce the date. I think you want:
SELECT COUNT(profileid)
FROM t
WHERE DATE(from_unixtime(timestamp)) = '2017-12-22' AND
profileid = '12345678';
I would be more inclined to write this as:
SELECT COUNT(profileid)
FROM t
WHERE profileid = '12345678' AND
timestamp >= UNIX_TIMESTAMP('2017-12-22') AND
timestamp < UNIX_TIMETAMP('2017-12-23');
This allows the query to make full use of an index on t(profileid, timestamp).
You can use MySQLs DAY, MONTH, and YEAR functions combined with FROM_UNIXTIME.
SELECT COUNT(profileid)
WHERE profileid='12345678'
AND DAY(FROM_UNIXTIME(timestamp))=22
AND MONTH(FROM_UNIXTIME(timestamp))=12
AND YEAR(FROM_UNIXTIME(timestamp))=2017;
A few things here.
You can convert your raw timestamp to a MySQL TIMESTAMP object with FROM_UNIXTIME(timestamp). You already know that.
Once you have a TIMESTAMP you can use all sorts of date functions on it.
You can convert the other direction with UNIX_TIMESTAMP()
When you're looking up records for one day you can do date range searching.
So your query should maybe be
SELECT COUNT(*) cnt
FROM t
WHERE profileid = '12345678'
AND timestamp >= UNIX_TIMESTAMP('2017-12-22')
AND timestamp < UNIX_TIMESTAMP('2017-12-23')
That will pick up every timestamp value on the day you want, up to but not including midnight on the next day. If you have an index on (profileid, timestamp) this kind of query will be fast.
Note you can also do
SELECT COUNT(*) cnt, profileid
FROM t
WHERE timestamp >= UNIX_TIMESTAMP('2017-12-22')
AND timestamp < UNIX_TIMESTAMP('2017-12-23')
GROUP BY profileid
and get a result set showing the counts for all profile ids for that day. And, you can do
SELECT COUNT(*) cnt, profileid, DATE(FROM_UNIXTIME(timestamp)) day
FROM t
WHERE timestamp >= UNIX_TIMESTAMP('2017-11-01')
AND timestamp < UNIX_TIMESTAMP('2017-12-01')
GROUP BY profileid, DATE(FROM_UNIXTIME(timestamp))
and get everything for November.
You can do this
SELECT COUNT(*) cnt, profileid, LAST_DAY(FROM_UNIXTIME(timestamp)) month_ending
FROM t
WHERE timestamp >= UNIX_TIMESTAMP('2017-01-01')
AND timestamp < UNIX_TIMESTAMP('2018-01-01')
GROUP BY profileid, LAST_DAY(FROM_UNIXTIME(timestamp))
and get a month-by-month summary for a whole year.
Date arithmetic is useful. That's why many table designs use actual datestamp-like fields, like DATETIME and TIMESTAMP, rather than raw integer timestamps.

PHP count mysql row with changed date format

I have next problem:
My table date format was: LIKE 2017-01-08 18:50:25 (with time).
When i use sql query like
'SELECT date FROM table WHERE date = "2017-01-08"'
My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.
Note, i will not change INSERT date time!
Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.
SELECT count(*) FROM table WHERE DATE(date) = CURDATE()
You can also replace CURDATE() with NOW(), CURRENT_DATE(), and CURRENT_DATE
You can also use it in the following way
'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'
the date_format is mysql function which return date according to your pattern the above pattern only return the Y-m-d from the datetime
I hope it will help you
plz change your statement equal operator to greater than
'SELECT date FROM table WHERE date > "2017-01-08"'
as by default if time portion is not present then it is putting 00:00...

mysql select date using timestamp

in my database i use a column named startdate and in the column there are rows with timestamps, looking like: 1410178260
Normally, when i use a datetime field and i want to select all the items with the date of today, i run this query:
$sql = "SELECT id FROM agenda2 WHERE DATE(startdate) >= CURRENT_DATE()";
But now, using the timestamps, i don't know how to make a query that selects all the items inserted today.
Can someone help me with that?
You need to convert to date using the function from_unixtime()
mysql> select FROM_UNIXTIME('1410178260');
+-----------------------------+
| FROM_UNIXTIME('1410178260') |
+-----------------------------+
| 2014-09-08 17:41:00 |
+-----------------------------+
So you may do as
SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()
If you're using unix timestamps then you've got to use
$sql = "SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()";
Consider that both versions can't make use of an index.
see FROM_UNIXTIME

mysql date and datetime problem

I have a mysql table called table1. with id and dt columns. The dt is of type datetime.
From this, I am trying to extract all id's with a specific date (not given the time, just the date, like 2011-02-11 say)
I could do it as a between the 00:00:00 and midnight of that date, but is their a proper way to do it with mysql and php?
SELECT id, dt
FROM table1
WHERE DATE(dt) = '2011-02-11'
MySQL's got a pretty comprehensive set of Date/Time handling functions, documented here

Categories