select timestamp from mysql - php

I have a problem with query where the conditions are related to TIMESTAMP column. So I have a table named table1 which has two columns start_time and end_time. Both of them store TIMESTAMP. I want to query my databse for a particular rows where end_time is higher than given timestamp value and lower than given timestamp value. Basically I want to get all the rows where end_time is between given TIMESTAMP range.
I have found that in my case following query works:
SELECT * FROM table1
WHERE UNIX_TIMESTAMP(end_time) >= FROM_UNIXTIME('2015-11-30 20:14:00')
AND UNIX_TIMESTAMP(end_time) <= FROM_UNIXTIME('2015-11-30 20:14:05')
But it is not what I want to. I want to give TIMESTAMP value instead of DATE.
What is also strange is that this query returns values that have end_time of earlier date than given timestamp:
SELECT * FROM table1 WHERE end_time > FROM_UNIXTIME( '2015-11-30 20:20:05' )
Query above returns rows where end_timestamp is 2015-11-30 20:18:05
I feel that I am missing something here. Should I use INT instead of TIMESTAMP for my end_time? I believe that the following query should return expected values but it gives me nothing:
SELECT * FROM table1
WHERE end_time >= FROM_UNIXTIME( 1448914740 )
AND end_time <= FROM_UNIXTIME( 1448914810 )

You don't need to convert your WHERE clause values using FROM_UNIXTIME. FROM_UNIXTIME converts a TIMESTAMP into a date. If you're sending values to the SQL statement that are already in TIMESTAMP format, your SQL will look like this:
SELECT * FROM table1 WHERE end_time >= 1448914740 AND end_time <= 1448914810
If you are sending date strings (e.g. '2015-11-30 20:14:00' and '2015-11-30 20:14:05') then your SQL would look like this:
SELECT * FROM table1 WHERE FROM_UNIXTIME(end_time) >= '2015-11-30 20:14:00' AND FROM_UNIXTIME(end_time) <= '2015-11-30 20:14:05'
Since you're storing your values in your table as TIMESTAMP I would recommend you use the first SQL statement.

Related

Average day values of last month as a SQL statement

I got a table with two columns, timestamp (like '1405184196') and value.
I've saved some measured values.
$day= time()-84600;
$result = mysql_query('SELECT timestamp, value FROM table WHERE timestamp >= "'.$day.'" ORDER BY timestamp ASC');
This is how I get all values for the last 24h.
But is it possible to get average day values for the last month with a SQL statement or do I have to select all values of the last month and calculate the average of each day via PHP?
Several issues with Anish's answer:
1) This won't work if date+time is being stored in the timestamp field.
2) It assumes the OP means last month i.e June, May etc and not the last say 30 days.
This solves those issues:
SELECT DATE(`timestamp`) as `timestamp`, AVG(value)
FROM table
WHERE `timestamp` >= CURDATE() - INTERVAL 1 MONTH
GROUP BY DATE(`timestamp)
EDIT
Since the timestamp is a unix timestamp and the OP would like a calendar month:
SELECT DATE(FROM_UNIX(`timestamp`)) as `timestamp`, AVG(value)
FROM table
WHERE MONTH(FROM_UNIX(`timestamp`)) = MONTH(NOW() - 1)
GROUP BY DATE(FROM_UNIX(`timestamp))
You can do this:-
SELECT timestamp, AVG(value)
FROM table
GROUP BY timestamp
HAVING MONTH(timestamp) = MONTH(NOW()) - 1;
This query calculates average for last month.
DEMO

how can i get record out by using date between certain columns

how can i get record out by using date between certain columns
Example.
I have a date value, 14-11-2013. I want the record in my tabel that has this value between startdate and end_date. I have tried with between statement and bigger/less then, but without sucess. Is this even possible?
This is my tabel: time_periode .. when i select *
id start_time start_date end_time end_date payment_t. payment_d file_ready_d file_ready_t
11 1385337600 25-11-2013 1386460800 08-12-2013 1387238400 17-12-2013 1386892800 13-12-2013
10 1384128000 11-11-2013 1385251200 24-11-2013 1386028800 03-12-2013 1385683200 29-11-2013
9 1382918400 28-10-2013 1384041600 10-11-2013 1384819200 19-11-2013 1384473600 15-11-2013
8 1381708800 14-10-2013 1382832000 27-10-2013 1383609600 05-11-2013 1383264000 01-11-2013
Desired output
id start_time start_date end_time end_date payment_t. payment_d file_ready_d file_ready_t
10 1384128000 11-11-2013 1385251200 24-11-2013 1386028800 03-12-2013 1385683200 29-11-2013
If your date is $time_period, then use BETWEEN:
SELECT * FROM t WHERE $time_period BETWEEN start_date AND end_date
Alternatively, you can use double-comparison:
SELECT
*
FROM
t
WHERE
$time_period>start_date
AND
end_date>$time_period
However, you should use proper format for your date as it's described in MySQL DATE manual page. If that's not impossible, then there is STR_TO_DATE() function:
SELECT
*
FROM
t
WHERE
STR_TO_DATE($time_period, '%d-%m-%Y')
BETWEEN STR_TO_DATE(start_date, '%d-%m-%Y')
AND
BETWEEN STR_TO_DATE(end_date, '%d-%m-%Y')
SELECT ...
FROM ...
WHERE '2013-11-14' BETWEEN start_date AND end_date
Note the yyyy-mm-dd format of the date. This is mysql's native date format, and is what lets this query work. It would appear your start_date and end_date fields are probably just varchars, in which case this query will fail, because 08-12-2013 is not a valid mysql date. You should always store dates/times in native date/time formats and fields.

Assistance with MySQL query & timestamps

I am trying to create a query which returns offers for all rows that belong to a club_id that are within a start_date & end_date, however the query should also return results for any that match the club_id AND the end_date is 0 - any ideas of how to do this?
My current query is below...
SELECT
* ,
UNIX_TIMESTAMP( start_date ) AS start_dateStamp,
UNIX_TIMESTAMP( end_date ) AS end_dateStamp
FROM
(`offers`)
WHERE
UNIX_TIMESTAMP( `start_date` ) <1329308797
AND
UNIX_TIMESTAMP( `end_date` ) >1329308797
AND
`club_id` =23
SELECT
`offers`.* ,
UNIX_TIMESTAMP( start_date ) AS start_dateStamp,
UNIX_TIMESTAMP( end_date ) AS end_dateStamp
FROM `offers`
WHERE `club_id` =23
AND (
(
`start_date`<FROM_UNIXTIME(1329308797)
AND `end_date`>FROM_UNIXTIME(1329308797)
)
OR `end_date`=FROM_UNIXTIME(0)
)
Please note, that I moved the convertion from unix-timestamp to MySQL-date from the field to the constant - this way it has to be converted only once, and not for all rows. Additionally this way an index can be used.
Edit
With "date zero" not being Unix-Zero but MySQL-Zeor the last line should be
OR `end_date`='0000-00-00'
additionally, if the data type of start_date and end_date is not DATETIME but DATE you need
DATE(FROM_UNIXTIME(...))
instead of
FROM_UNIXTIME(...)
you didn't specify if end_date = 0 also reqires start_date > $timestamp, so I assumed start_date still has to meet its criteria.
SELECT
please_name,
the_columns,
you_want_to_select_seperately,
for_reasons,
UNIX_TIMESTAMP(start_date) AS start_dateStamp,
UNIX_TIMESTAMP(end_date) AS end_dateStamp
FROM `offers`
WHERE `club_id` = 23
AND `start_date`) < UNIX_TIMESTAMP(1329308797)
AND (`end_date` > UNIX_TIMESTAMP(1329308797) OR `end_date` = "0000-00-00")
Some notes on your SQL:
don't use * unless you have reasons. which, judging from your original question, you really really don't
try to format your code so it's readable
sort your conditions so the condition selecting the fewest records (read: has the highest specificity) comes first. This has become more a mental thing than hinting the MySQL optimizer, it's still a convention I'd stick to
if you compare (date) ranges, consider using col BETWEEN min_val and max_val (boundaries are inclusive)
don't use functions in the WHERE and GROUP BY clause. MySQL cannot cache/index these and must thus run the value of every row through that function. UNIX_TIMESTAMP() has a friend named FROM_UNIXTIME() - doing pretty much the exact opposite. This is nothing you can't do yourself in PHP with date(), though.
consider setting fields to NULL if they have an unknown value. You either have a date (2012-02-15) or you don't (NULL). That would allow your query to simply check OR end_date IS NULL
Is this what you want?
SELECT *
, UNIX_TIMESTAMP(start_date) AS start_dateStamp
, UNIX_TIMESTAMP(end_date) AS end_dateStamp
FROM
`offers`
WHERE
(UNIX_TIMESTAMP(`start_date`) < 1329308797 AND UNIX_TIMESTAMP(`end_date`) > 1329308797)
OR
(`club_id` = 23 AND `end_date` = '0000-00-00 00:00:00')

SQL and date comparison

I have a sql statement, i select a few items, but I need to make sure that the date i have saved is greater then the current date.
SELECT * FROM table WHERE column_date > current_date
How do I make the code over, working?
Column date is saved like this 0000-00-00 00:00:00 (usual save, in other words).
This is what I try to achieve: AND time > current_date OR time == NULL But it ain't working. The time column is currently 0000-00-00 00:00:00 which is NULL, right?
Or how can I do, it must be greater than or equal to 0000-00-00 00:00:00
If you are using MySQL, NOW() should do the trick.
SELECT * FROM table WHERE column_date > NOW()
If you want to eliminate the time value and just compare to date value, following could be used:
SELECT * FROM table WHERE column_date > CURDATE()
You could use the Mysql Function NOW():
SELECT * FROM table WHERE column_date > NOW()
As per this documentation article, 0000-00-00 is a "dummy date" that can be used instead of NULL, which means 0000-00-00 itself is not NULL.
Given that 0000-00-00 cannot be greater than any other date, you should use either a condition with OR:
SELECT * FROM table WHERE column_date > NOW() OR column_date = '0000-00-00'
or a union:
SELECT * FROM table WHERE column_date > NOW()
UNION ALL
SELECT * FROM table WHERE column_date = '0000-00-00'
Alternatively you could use a construct like this:
SELECT *
FROM table
WHERE IFNULL(NULLIF(column_name, '0000-00-00'), '9999-12-31') > NOW()
But that would probably prohibit the query from taking advantage of the index on column_date, if any.

Select records from current month

I have a table with a varchar field called slldate. This field contains dates in this format:
2010-08-30
(YYYY-MM-DD)
Now I would like to select the records containing the current month using a mysql query.
Anyone?
as you used a char field instead a date field, you have to cast the value and then use the normal date functions. like
SELECT * FROM table WHERE MONTH(CAST(slidate as date)) = MONTH(NOW()) AND YEAR(CAST(slidate as date)) = YEAR(NOW())
Try this:
SELECT * FROM table_name WHERE MONTH(slldate) = date('m') AND YEAR(slldate) = date('Y');
I have a table with a varchar field called slldate. This field contains dates
Then you should change it to a date field. You (should) know it's the right thing to do.
The method below will work with your varchar strings - but can be simplified somewhat for dates.
Note that since your date strings are already big-endian, you don't need to cast them and loose the benefit of index optinmisation:
SELECT *
FROM yourtable
WHERE slldate >= CONCAT(DATE_FORMAT('%Y-%m-', NOW()),'01')
AND slldate < CONCAT(DATE_FORMAT('%Y-%m-', NOW() + INTERVAL 1 MONTH), '01')
SELECT * FROM table_name WHERE YEAR(savedate) = YEAR(CURDATE()) AND MONTH(savedate) = MONTH(CURDATE())

Categories