im trying to select all the rows having a particular year in mysql.
the datetime is in a format such as 2016-12-02 10:00:00 so i am trying to use wildcard to do it. However i tried
SELECT * FROM all-data.prices where YEAR(timestamp)= "2005";
and SELECT * FROM all-data.prices where timestamp= "2005%" ;
and it didnt work for me.
Any suggestion on which is the correct syntax for such select statement?
It is not the best idea to use a FUNCTION on a field in the WHERE Clause. Then MySQL must calculate YEAR(timestamp) from every ROW before it can compare. So it is a FULL TABLE SCAN and cant use a INDEX.
A better was is to compare the timestamp with a Range
SELECT * from your_table WHERE `timestamp` >= '2015-01-01 00:00:00' AND `timestamp` < '2016-01-01 00:00:00';
Then MySQL can use a INDEX (if there is one)
To use % you also must use the LIKE Keyword
SELECT '2015-01-01 23:50:00' LIKE '2015%';
in your sample
SELECT * from your_table WHERE `timestamp` LIKE '2015%';
Related
I have a table with a datetime column which is indexed. I'd like to select all the rows for a specific date. Previously I've been using this:
SELECT *
FROM `table`
WHERE DATE(date_column) = '2016-04-12';
This works fine, however I've been noticing performance issues due to the fact that MySQL does not make use of the index once I apply the DATE() function. I've also tried:
SELECT *
FROM `table`
WHERE SUBSTR(date_column,1,10) = '2016-04-12';
to no avail. So far the only thing that seems to work is:
SELECT *
FROM `table`
WHERE date_column >= '2016-04-12'
AND date_column < '2016-04-13'
This last query successfully uses the index but I'd like to know if there is a way to do this with only a single condition.
I like this code pattern; it is clear, precise (no confusion over midnight), and can use the index:
WHERE date_column >= '2016-04-12'
AND date_column < '2016-04-12' + INTERVAL 1 DAY
It even works 'correctly' for DATE, DATETIME, TIMESTAMP, DATETIME(6), etc.
'2016-04-12 00:00:00.000' is identical to '2016-04-12' when used in this context. I prefer the latter because it is more concise.
Here is my MySQL query.
SELECT Timestamp,a_number,Verdict,OrderId
FROM Main
WHERE Timestamp between DATE '2014-03-12' AND '2014-03-11'
and a_location like '%london%';
In above query Timestamp is one of the field in Main table. But when i execute the query it is executing and getting the output as empty set.
When I checked Timestamp values that stored in database i am getting the values that are given below
1323517427743,1323867674980.
What is the mistake that i have done. And what should i make change to get my need.
col BETWEEN a AND b is a syntactic sugar over col >= a AND col <= n - i.e., you must use the correct order:
SELECT Timestamp,a_number,Verdict,OrderId
FROM Main
WHERE Timestamp between DATE '2014-03-11' AND '2014-03-12'
AND a_location like '%london%';
Try using back ticks around the DATE column
SELECT Timestamp,a_number,Verdict,OrderId
FROM Main
WHERE Timestamp between `DATE` '2014-03-12' AND '2014-03-11'
and a_location like '%london%';
If the Timestamp's column type is timestamp, then you could just do:
SELECT `Timestamp`, a_number,Verdict,OrderId
FROM Main
WHERE `Timestamp` between '2014-03-11' AND '2014-03-12'
and a_location like '%london%';
If it is integer, then you should do:
SELECT `Timestamp`, a_number,Verdict,OrderId
FROM Main
WHERE `Timestamp` between UNIX_TIMESTAMP('2014-03-11') AND UNIX_TIMESTAMP('2014-03-12')
and a_location like '%london%';
And for between A and B, A should less or equal than B, or no result will be returned.
I have 2 PDO database connections. I am doing a search within a MS SQL table for a row that closest matches a date (mysql datetime) row.
I have mysql.table1.date passed to mssql.table and I am looking for the closest date accordingt to the mssql.table.date. It is also defined as a datetime field. I only need 1 row returned, the closest to the time, so in essence:
SELECT * FROM table ORDER BY CLOSEST(mysqldate = mssql.table.date) LIMIT 1;
I know the syntax above is incorrect but that basically outputs what I need, but I really do not know how to do this with mssql.
Any help?
Basically u can find the difference of the mysql date with all the dates in mssql.Table.Date column .Then u need to select the least difference value from the above query .Hopefully the below query might help u
;with CTE as
(
Select mssql.table.date,row_number()
over (order by abs(datediff(day,mysqlDate,mssql.table.date))) rowNumber
from mssql.Table)
select mssql.table.date from CTE where rowNumber=1
A simple solution which worked for me was to do the following:
SELECT * FROM `table` WHERE `date` < `startDate` ORDER BY `date` LIMIT 1;
This returns 1 row matching the closest time to the time I am passing :)
Tried this
select * from table where timestamp_field between 1330560000 and 1336170420
and this
select * from table where timestamp_field >=1330560000 and timestamp_field<=1336170420
both returning empty result set.
But this
select * from table where timestamp_field >= 1330560000
returns all the rows
To make things more absurd
select * from table where timestamp_field <= 1336170420
returns empty result set.
Of course, there exists timestamp values before, between and after 1336170420=4.may 2012. and 1330560000=1.march 2012.
Timestamp values are ok, at least phpmyadmin shows correct (human-readable) date-time values.
I created timestamps by parsing strings, with
UPDATE table SET timestamp_field = STR_TO_DATE(timestamp_string, '%d.%m.%Y')
Guess I'm missing something, but can't find what!?
MySQL expects date literals, not integer ones:
SELECT *
FROM table
WHERE DATE(timestamp_field) BETWEEN '2012-03-01' AND '2012-05-04'
To use integers (assuming that they are seconds since the UNIX epoch), first convert them using MySQL's FROM_UNIXTIME() function:
SELECT *
FROM table
WHERE timestamp_field BETWEEN FROM_UNIXTIME(1330560000)
AND FROM_UNIXTIME(1336170420)
Or else use UNIX_TIMESTAMP() to convert your column to its UNIX representation:
SELECT *
FROM table
WHERE UNIX_TIMESTAMP(timestamp_field) BETWEEN 1330560000 AND 1336170420
why not convert the timestamps from_unixtime(timestamp/1000) as dateTime while using your sql code? have you tried that?
I would like to so a nested mysql request that is counting actions for each day like this:
SELECT `timestamp` AS `date`,
(SELECT COUNT('id')
FROM `actions` WHERE `timestamp` = `date`) AS `action_num`
FROM `actions`;
That would work great if it wasn't so that I need to calculate each day and all I got is each timestamp. If it was only a date it would work. So I thought if I can take out the date from the string and compare it this might work:
SELECT `timestamp` AS `date`,
(SELECT COUNT('id') FROM `actions`
WHERE `timestamp` REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' = `date`
REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}') AS `action_num`
FROM `actions`;
But it did not work.
Any ideas on how to compare the dates from two timestamp fields directly in MySQL?
Thanks
Very easy, the inner query should be:
SELECT count(id) FROM actions
WHERE DATE(`timestamp`) = DATE(`date`) as action_num
See: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
You can use DATEDIFF() function to compare the dates in MySQL,
and TIMESTAMPDIFF() for comparing timestamps , DATE() to get date information from timestamp.
More date and time functions and examples are available in MySQL's reference manual pages.