MySQL where query by hour diff of 2 timestamps - mysql

I have a table with 2 timestamps: start_time and end_time. How can I query with conditions like select all where the diff of those 2 fields is more than X hours.
Also does the field type (timestamp vs datetime) has any impact on the query i'm trying to achieve?

The data type of the fields does for sure have some differences.
As stated in MySQL Timestamp Difference, it is usually the case that, given two datetime fields, they get converted to timestamps in order to subtract them.
The query could be something like
SELECT *
FROM Table
WHERE TIMESTAMPDIFF(HOUR,start_time,end_time)>2
Edit:
What written above is valid for MySQL, you can use DATEDIFF if you are working in SQL Server

Related

Mysql optimizing date filter

Is there any difference in any sql engines (and particularly in mysql) in the following two queries?
SELECT * FROM table where date = '2019-01-01'
And:
SELECT * FROM table where date = DATE('2019-01-01')
Doing an explain returns the same result, but perhaps there's some sort of difference that I'm not catching? I need to run a query against a multi-billion row table and am trying to optimize it before running.
There should not be. The expression DATE('2019-01-01') should be evaluated during the compilation phase turning the result into a date. Similarly, the constant value '2019-01-01' is implicitly converted to a date for the comparison.
This allows MySQL (and most other databases) to use indexes and partitions defined on that column.
Date() function Extracts the date part of a date or date/time expression
for example the value of the field name BirthTime is "2017-09-26 16:44:15.581"
so you have to use the following query to check the date :
SELECT DATE(BirthTime)
result is : 2017-09-26

MySQL select from table with unixtime row, but using datetime in query

I have a MySQL table, one of rows is filled using current unixtime (let's call it 'unixtime').
I need to select values, but using datetime format in the query.
For example:
SELECT * from test where unixtime>"2017-01-06 12:00" AND unixtime<"2017-01-08 12:14"
Note: Datetime range could be different.
Thank you very much!
As mentioned above, by using FROM_UNIXTIME() in your query. The following query selects from a table called 'testValues' and a Unix time field in the table, which is called 'timevalue' (stored as an int). It selects between two date ranges with the ranges in DateTime format in the query.
SELECT * FROM testValues
where FROM_UNIXTIME(timevalue) > "2017-01-07 21:00:48"
AND FROM_UNIXTIME(timevalue) < "2017-01-07 21:02:09"
Hope that helps :)

Mysql select where date between 'yy/dd/MM' and 'yy/dd/MM'

I am stuck in a situation where I am reading data from CSV file through 'LOAD DATA LOCAL INFILE' and storing it in Mysql table.
The date column in my table is of type string.
The below query is not working if my Date format is 'yy/dd/MM', it only returns 2 records
select column1, column2 from myTable where date between '16/08/15' and '16/08/20';
and if I ran this:
select column1, column2 from myTable where date > '16/08/15';
It return all records.
Is there a way to ran the first query so I can specify start and end date ?
Turn your dates into 'yyyy-mm-dd' format before using them inside queries, and it will be easier.
Instead of passing the year as yy try to pass it as yyyy in the date string since my sql stores dates in that format.Since every year is a four digit one, giving a two digit year in the where clause in order to pull out all records greater than that year will actually pull out all the records.
Since you say your date column is of type "String" and not DATE so in your query mysql is not operating the between function on dates, instead it's operating it on strings. You probably need to cast you string dates to actual dates before applying the between function. see e.g. str_to_date function.

Comparing dates in sql Phpmyadmin

i am trying to run a query in mysql to select all records from a table where the "end_date" of that record is greater than or equal to the current date, but it keeps coming up with "MySQL returned an empty result set". But such a record definitely exists n my table, is there a problem with my query?
this is the value in the end_date column for the one record "03/24/2014".
and this is my query
SELECT * FROM ******** WHERE DATE(end_date) > DATE(CURDATE())
I have my suspicions however that it may be the date format meaning the date format on my computer might be like this: dd/MM/yyyy
Whereas the date format on the server might be like this : MM/dd/yyyy
Unfortunately if that is the case i dunno what to do about that yet.
As noted in comment by #FreshPrinceOfSO, better to store date as real DATE, TIMESTAMP, or DATETIME.
If that's not an option--it's not your db to alter for example--then Mysql has a STR_TO_DATE() function you can use:
SELECT * FROM ********
WHERE STR_TO_DATE(end_date,'%m/%d/%Y') > CURDATE();
HOWEVER...
If end_date column has an index, the function applied to it will nullify the index benefit.

slow sql query - selecting data depending on date interval

I have a query that is causing me some trouble. I'm wondering if there is a better way to write this SQL;
SELECT * FROM report
WHERE blogid = 1769577
AND DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= datetime
so as its faster to fetch the results.
Thanks in advance.
I don't see anything wrong with the query, but you could make sure to have indexes on the blogid and datetime columns
If your table is huge, you might consider horizontal partitioning, which can have a significant impact on performance. See http://dev.mysql.com/tech-resources/articles/performance-partitioning.html
In Oracle SQL, you can do date arithmetic through +/- operators. I don't know if it would work in MySQL, but you might as well try doing datetime >= (CURDATE() - 30).
SELECT * FROM report WHERE blogid = 1769577 AND datetime >= (curdate() - 30)
Edit: This blog entry seems to confirm my suggestion: http://mysql-tips.blogspot.com/2005/04/mysql-date-calculations.html
I suggest using the internal datediff() function instead of subtract the interval. like this:
datediff(curdate(), datetime) < 30
so the query is:
SELECT * FROM report WHERE blogid = 1769577 AND datediff(curdate(), datetime) < 30
Guess 1: your blogid is not an int column. Then you can read the MySQL manual:
Comparison of dissimilar columns may
prevent use of indexes if values
cannot be compared directly without
conversion. Suppose that a numeric
column is compared to a string column.
For a given value such as 1 in the
numeric column, it might compare equal
to any number of values in the string
column such as '1', ' 1', '00001', or
'01.e1'. This rules out use of any
indexes for the string column.
Guess 2: your blogid is not indexed.
Guess 3: the reports table is myisam. In this case when you modify data MySQL uses table level locking on the whole reports table. You say that every time a blog is viewed a new record is added. These frequent updates may cause table level locking and slow down your select queries.
Otherwise your query is fine.
Cheers!