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
Related
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.
i want to get past and future events from database and by using php-mysql query, and i had try with this query
select * from table name where date >date();
but i got the past events in future section. and i saved date in varchar data type.
please help me
Thank You
Use NOW() MySQL function to get current timestamp and STR_TO_DATE function to convert string date to date format
select * from table_name where STR_TO_DATE(date, '%Y-%m-%d %H:%i:%s') > now();
But you really should consider to alter your table to TIMESTAMP data type
I have a PHP scirpt that is always querying all the data from a database table and it's getting pretty slow. I really just need the data of a specific month and year.
Is there a simple way to get only those entries? For example, everything from February 2013?
The column that stores the dates in my table is of type datetime, if that applies to the solution.
You can add that condition in the WHERE clause of your select statement. I would recommend using BETWEEN operand for two dates:
SELECT myColumns
FROM myTable
WHERE dateColumn BETWEEN '2013-02-01' AND '2013-02-28';
If you mean to say you want everything beginning with February 2013, you can do so using the greater than or equal to operator:
SELECT myColumns
FROM myTable
WHERE dateColumn >= '2013-02-01';
EDIT
While the above are my preferred methods, I would like to add for completeness that MySQL also offers functions for grabbing specific parts of a date. If you wanted to create a paramaterized query where you could pass in the month and year as integers (instead of a start and end date) you could adjust your query like this:
SELECT myColumns
FROM myTable
WHERE MONTH(dateColumn) = 2 AND YEAR(dateColumn) = 2013;
Here is a whole bunch of helpful date and time functions.
You should index the datetime field for added efficiency and then use Between syntax in your sql. This will allow the mysql engine to remove all records that you are not interested in from the returned data set.
I have a MySQL database. In a couple of tables, the information that gets stored needs to be retrievable by week. So, I want to be able to do a SELECT FROM *database* WHERE week = *week*. The problem that I have is that the week part is stored as a unix timestamp (to allow for more versatilty like getting the date and time, just time, etc...).
So the question: How can I retrieve this record WHERE date = *date* when the stored date is a unix timestamp and date I'm matching it against is not?
If my question is too confusing and something needs to be rephrased or said in a clearer manner please comment and let me know.
MySQL has a built-in WEEK() method for handling dates: MySQL WEEK() Reference
Unfortunately however, MySQL's WEEK() method only supports DATE datatypes rather than a UNIX TIMESTAMP. Therefore, we must first convert the timestamp to a date so we can then pass that date to the WEEK() method:
SELECT
*
FROM
my_table
WHERE
WEEK(
DATE_FORMAT(
FROM_UNIXTIME('my_unix_timestamp_col'),
'%e %b %Y'
)
) = 51
If you have a column which is the DATE data-type, the query can be simplified (and can also use indexes):
SELECT * FROM my_table WHERE WEEK(my_date_col) = 51
I have a mysql table that has a column for "date time" in TIMESTAMP format. Is there a way to group the rows by day using that column? And in an SQL query, not grouping them in php.
Assuming your column is one of the Date and Time types:
SELECT ... GROUP BY DATE(`datetimecolumn`)
GROUP BY TO_DAYS(`datetime_column`)
If you mean you have a standard DATETIME column, you can group by using one of the functions described here.
For instance:
GROUP BY DATE(datetime_column);
If you actually have a TEXT or VARCHAR column (your answer doesn't specify), then you'll need to convert it to to a date first:
GROUP BY DATE(STR_TO_DATE(datetime_column));