I have the following MySQL query:
SELECT * FROM m3
WHERE datetime >= DATE_SUB(NOW(), INTERVAL 8 HOUR) AND mod(minute(time),1) = 0
ORDER BY id ASC ";
The output for this query is for example now: 18:30:34
Every minute there will be a new datetime, and I will select only the full (half) hours. So 18:30, 19:00, 19:30, 20:00 etc.
Now I want only select the hours and minutes without the seconds included.
So for example: 18:30.
How can I manage it to have it in this query?
Try SELECT DATE_FORMAT(datetime, '%H:%i') ...
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Related
Given a set of data with date_created stored like 2017-04-13 23:29:52, how would I construct an SQL query to select all items that were created within the last 3 hours?
I originally thought to do something like this:
SELECT
*,
MAX(date_created)
FROM items
GROUP BY date_created
but that would not be exactly what I want. I'm not sure how to go about this.
Use NOW() and INTERVAL in your WHERE clause
SELECT * FROM items WHERE date_created <= NOW() - INTERVAL 180 minute AND date_created >= NOW() - INTERVAL 210 minute
This one uses CURDATE, CURDATE and DATE_ADD:
SELECT *
FROM items
WHERE DATE(date_created) = CURDATE()
AND TIME(date_created) BETWEEN CURTIME() AND DATE_ADD(CURTIME(), INTERVAL -3 HOUR)
select * from items where
extract(hours from age(current_timestamp, date_created))>=3;
Extract keyword would extract hours difference from current timestamp and return only that is greater than or equal to 3.
my query is running about 10 seconds and that's unacceptable.
I am looking for a way to improve this speed but i'm out of options.
I have to find the records between now and 30 days back in a table over 12 million rows.
The following query:
SELECT DATE(DATE) AS FDATE,
SUM(VIEW_COUNT) AS COUNT,
COUNT(IP_ADDRESS) AS CLIENTS
FROM VIEWS
WHERE USERID = 'test'
AND DATE BETWEEN ADDDATE(CURDATE(), INTERVAL -30 DAY) AND CURDATE()
GROUP BY FDATE DESC
I also tried, but the same effect:
SELECT DATE(DATE) AS FDATE,
SUM(VIEW_COUNT) AS COUNT,
COUNT(IP_ADDRESS) AS CLIENTS
FROM VIEWS
WHERE USERID = 'test'
AND DATE >= (DATE(NOW() - INTERVAL 30 DAY) + INTERVAL 0 SECOND)
GROUP BY FDATE DESC
You could try using MySQL Indexes for a better performance in decreasing the query execution time.
http://dev.mysql.com/doc/refman/5.7/en/mysql-indexes.html
I would like to output recent data that was entered to the DB in the last Three hours, for that I have done this :
SELECT * FROM `tableName` WHERE DATE <= TIMEDIFF ( 'SYSDATE()', '03:00:00' )
But it does NOT worked for me, any ideas on how I can do it ?
You can do this :)
SELECT *
FROM tableName
where Date >= DATE_SUB(NOW(),INTERVAL 3 HOUR);
use timestampdiff on your date column.
SELECT * FROM tableName
WHERE timestampdiff( HOUR, date, now() ) <= 3
Provided your column date must be of type datetype or timestamp
I have tried to filter records but with the use of now function as given below
select * from table where date>= DATE_SUB( NOW( ) ,INTERVAL 90 DAY )
What I need is a select statement that can filter its records for a week or month from the current date but without using NOW() function
if you are using java you could make use of the following code
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
or you could use curdate() of mysql
Since I found it hard to understand the question I provide the following possibilities:
Try for all dates in a week from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 WEEK)
and for all dates in a month from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 MONTH)
If you are looking for all dates of the current month use
SELECT * FROM table
WHERE MONTH(date)=MONTH(CURDATE()) AND YEAR(date)=YEAR(CURDATE())
or for all dates of the current week use
SELECT * FROM table
WHERE WEEK(date)=WEEK(CURDATE()) AND YEAR(date)=YEAR(CURDATE())
I would like to rows that have only been entered in the last 1 day.
I have a date column which stores YYYY-MM-DD, and I allow the user to send a date that they want to look at in this format yyymmdd how can I use this data to limit it to the previous day only?
I would imagine it is something to do with the BETWEEN keyword but I cant figure it out.
SELECT * from TABLE_NAME WHERE ROW_DATE BETWEEN '2011-03-20' AND '2011-03-21'
This query:
SELECT *
FROM mytable
WHERE mydate >= STR_TO_DATE('110321', '%y%m%d') - INTERVAL 1 DAY
AND mydate < STR_TO_DATE('110321', '%y%m%d')
will return all records for Mar 20, 2011
From the MySQL manual (here):
SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date_col;
SELECT * FROM YourTable WHERE date_column = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
This returns all rows for today and yesterday.