MySQL Top10 for 7 Days Ago - mysql

I have a problem...
I search in this site for any solutions... I tried them but on one workout :(
So I'm trying to get the top 10 results for 7 days ago by views...
So I try codes like that:
SELECT * FROM `data`
WHERE cast(`date` as DATE) BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND CURDATE() ORDER by `viewed` DESC LIMIT 0,10
or
SELECT * FROM `data`
WHERE `date` > (NOW() - INTERVAL 7 DAY)
ORDER by `viewed` DESC LIMIT 0,10
or
SELECT * FROM `data`
WHERE DATE(`date`) = DATE_SUB(NOW(), INTERVAL 7)
ORDER by `viewed` DESC LIMIT 0,10
or
SELECT * FROM `data`
WHERE `date` >= SUBDATE(NOW(), INTERVAL 7 DAY)
ORDER by `viewed` DESC LIMIT 0,10
I try them with any combination with NOW() TIME() DATE() CURDATE() SUBDATE() SUBTIME() DATE_SUB() etc... but nothing works :( I really don't know what is the problem. I submit the date to database with time() function(PHP) can it be that the problem?

Try this:
SELECT date, viewed FROM data
WHERE date BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 7 day) AND UNIX_TIMESTAMP(NOW())
ORDER BY viewed DESC
LIMIT 0,10;
And this if you want the date and time displayed.
SELECT FROM_UNIXTIME(date), viewed FROM data
WHERE date BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 7 day) AND UNIX_TIMESTAMP(NOW())
ORDER BY viewed DESC
LIMIT 0,10
Sample data:
CREATE TABLE data
(
id int auto_increment primary key,
date varchar(10),
viewed int
);
INSERT INTO data
(date, viewed)
VALUES
(1392749561, 50),
(1392749950, 25),
(1392850985, 10),
(1393023471, 75),
(1392936840, 100);
SQLFiddle demo

Related

Check if last update time is greater than 10 minutes

I have the following query:
SELECT * FROM event_incidents order by last_update desc limit 1;
What I want is to get the first row and check if the last_update time on that row is greater than 10 minutes from the current time.
You can try like this:
select * from event_incidents
where last_update >= (NOW() - INTERVAL 10 MINUTE)
ORDER BY last_update desc
LIMIT 1;
You can do this using aggregation and a comparison in the where. If you have an index on last_update then:
SELECT (case when MAX(last_update) >= date_sub(now(), interval 10 minute)
then 'recent'
else 'ancient'
end)
FROM event_incidents ;
I'm not sure what you want to return, so I made up "recent" and "ancient".
Note:
If you just want a flag on a single row being returned:
SELECT ei.*,
(last_update > date_sub(now(), interval 10 minute)) as RecencyFlag
FROM event_incidents
ORDER BY last_update desc
LIMIT 1;

mySQL rows from today not last 24 hours

Basically, I've got a query which looks like the following
SELECT
*
FROM
`transactions`
WHERE
`date` > DATE_SUB(NOW(), INTERVAL 1 DAY)
AND
`status` = '1'
How can I do it so I can get it from just today, not the past 24 rolling hours; the same for a week & month etc..?
Use DATE() to get the date of your date column and compare it to today's date using CURDATE()
WHERE
DATE(`date`) = CURDATE()
Use
SELECT *
FROM `transactions`
WHERE `date` >= curdate() AND `date` < curdate() + interval 1 day
AND `status` = '1'
which can make use of indexes to speed up the query

How to limit data to only the last 5 results

I have the query below to fetch the number of row and group them by week. It works great.
SELECT
WEEKOFYEAR(searched_on) AS weekno,
COUNT(*) AS num_search,
SUBDATE(searched_on, INTERVAL WEEKDAY(searched_on) DAY), INTERVAL + 0 DAY AS date_of_week,
FROM table
GROUP BY WEEK(DATE_SUB(searched_on, INTERVAL 1 DAY)) ORDER BY weekno ASC
How can I modify this in order to select only the last 5 results without using ORDER BY weekno DESC LIMIT 5 in order not to alter the way data has been arranged. Thanks
You could add the ORDER BY weekno DESC LIMIT 5 and push the query into a subquery, then have the outer query reorder ascending:
SELECT * FROM (
SELECT
WEEKOFYEAR(searched_on) AS weekno,
COUNT(*) AS num_search,
SUBDATE(searched_on, INTERVAL WEEKDAY(searched_on) DAY), INTERVAL + 0 DAY AS date_of_week,
FROM table
GROUP BY WEEK(DATE_SUB(searched_on, INTERVAL 1 DAY))
ORDER BY weekno DESC LIMIT 5
) inner
ORDER BY weekno
Simply use the limit used in mysql.
add the limit 5 after the code ORDER BY weekno ASC. means add it at the end of query.
Here is the tutorial.
Example
Use the below query:
SELECT
WEEKOFYEAR(searched_on) AS weekno,
COUNT(*) AS num_search,
SUBDATE(searched_on, INTERVAL WEEKDAY(searched_on) DAY), INTERVAL + 0 DAY AS date_of_week,
FROM table
GROUP BY WEEK(DATE_SUB(searched_on, INTERVAL 1 DAY)) ORDER BY weekno ASC
LIMIT 5 OFFSET (num_search-5) //add this line

Select between dates issues

I have this SQL:
$sql="SELECT *
FROM table
WHERE expiresdate >= Date(Now())
AND expiresdate <= Date_add(Date(Now()), INTERVAL 10 day)
ORDER BY expiresdate ASC";
it should basically show all rows in the database that are going to expire within 10 days time however, lets say the expiredate was 2013-03-06 - this row will not display on any day after the expiredate
does anyone have any ideas?
This should be what you need:
SELECT
*
FROM
`table`
WHERE
expiresdate <= CURDATE() + INTERVAL 10 DAY
ORDER BY
expiresdate ASC

mysql select 3days records from today

SELECT * FROM `user`
WHERE name !='' AND `date_created` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 3 Day )
AND DATE_SUB( CURDATE( ) ,INTERVAL 0 Day )
ORDER BY `date` ASC
The above query brings record 3day before from todays date.
but i need 3day records from today,which means tomorrow , day after tomorrow etc.
date_created is mysql date format.
SELECT * FROM `user`
WHERE name !=''
AND `date_created` BETWEEN curdate() and curdate() + interval 3 day
ORDER BY `date`
I have created this will return previous 3 days record
select * from events where DATEOFEVENT IN (select date(curdate()-3 ))