Mysql get data by year with pagination - php

I need to get the data based on year with pagination,if the rows count is less,then search in next year
SELECT *
FROM `user_notifications`
WHERE DATE_FORMAT(created_at, '%Y') = '2019'
ORDER BY `created_at` DESC
LIMIT 0,10
if rows count is less than 10,then search by year 2018

Given that your results are sorted by created_at DESC, I don't think a WHERE clause is necessary at all. If there are insufficient results from 2019, your query will automatically return results from 2018, 2017, 2016 etc. as necessary to get to 10 rows:
SELECT *
FROM `user_notifications`
ORDER BY `created_at` DESC
LIMIT 10

I think you just need a WHERE clause, to include both 2018 and 2019:
SELECT *
FROM user_notifications
WHERE YEAR(created_at) IN (2018, 2019)
ORDER BY created_at DESC
LIMIT 10;

If I've read it right, this sounds like just a simple ordering exercise
SELECT *
FROM user_notifications
WHERE created_at < :input_year + INTERVAL 1 YEAR
ORDER BY created_at DESC
LIMIT :offset, 10;
If you only want to go back one year you can just add another condition
SELECT *
FROM user_notifications
WHERE created_at < :input_year + INTERVAL 1 YEAR
AND created_at >= :input_year - INTERVAL 1 YEAR
ORDER BY created_at DESC
LIMIT :offset, 10;
Not that using a function like YEAR() on created_at rather than a comparison (<,>=) will prevent the engine from using an index on created_at

Use https://dev.mysql.com/doc/refman/8.0/en/year.html
SELECT *
FROM user_notifications
WHERE YEAR(created_at) <= $searchYear
ORDER BY created_at DESC
LIMIT 0, 10;

Related

Select Distinct records from a Timestamp row using day only from MySQL table

I'm trying to query only distinct dates from my table (ignoring the times) which uses timestamp for the date format (should I use a better format?). Here is my query, but it doesn't seem to work:
$query = "
SELECT DISTINCT DATE(event_date)
FROM schedule
WHERE DATE(event_date) >= CURDATE()
ORDER BY event_date ASC LIMIT 4
";
"event_date" is my timestamp row in the database.
You may have a problem with the order by. How about this?
SELECT DATE(event_date)
FROM schedule
WHERE event_date >= CURDATE()
GROUP BY DATE(event_date)
ORDER BY DATE(event_date) ASC
LIMIT 4;

sql order by most recent dates

I have this table in my database:
INSERT INTO `shop_stats` (`date`, `value`) VALUES
('09/2014', 326),
('08/2014', 1007),
('07/2014', 1108),
('06/2014', 1027),
('05/2014', 895),
('04/2014', 650),
('03/2014', 683),
('02/2014', 563),
('01/2014', 499),
('12/2013', 568),
('11/2013', 522),
('10/2013', 371),
('09/2013', 347),
('08/2013', 376),
('07/2013', 418),
('06/2013', 567),
('05/2013', 357);
i need to find a way to display the last 12 months.
I tried this:
SELECT * FROM shop_stats ORDER BY date DESC LIMIT 12
But it doesn't work correctly.
Any suggestions ?
SELECT * FROM shop_stats WHERE date >= DATE_SUB(NOW(),INTERVAL 1 YEAR) LIMIT 12
Your "dates" are stored as strings, presumably with the month first. So, the following order by should work
order by right(date, 4), left(date, 2)
You need to put the year before the month for ordering purposes.
If you want the last twelve months, I would recommend:
where right(date, 4) * 12 + left(date, 2) >= year(now()) * 12 + month(now())
order by right(date, 4), left(date, 2)
The where statement converts the dates to a number of months, for both the "date" column in your data and for the current time.
You can simply use STR_TO_DATE like this
SELECT
*
FROM
shop_stats
order by
STR_TO_DATE(date, '%m/%Y') DESC LIMIT 12
Demo
I suppose your field date has a type string
So you try this:
SELECT * FROM shop_stats
ORDER BY SUBSTRING(date, 4, 4) desc,
substring(date, 1, 2) DESC LIMIT 12
Show Sql Fiddle
You can take a look at DATE_SUB
SELECT * FROM shop_stats where date >= DATE_SUB(now(), INTERVAL 12 MONTH) ORDER BY date
Edit:
How about converting the string to date & doing the appropriate date operations in the sql ?
SELECT DATE
,t1.value
FROM (
SELECT DATE
,STR_TO_DATE(CONCAT (
'01/'
,DATE
), '%d/%m/%Y') date_
,value
FROM shop_stats
) t1
WHERE t1.date_ >= DATE_SUB(now(), INTERVAL 12 MONTH)
ORDER BY t1.date_ DESC
http://sqlfiddle.com/#!2/2be05/8
select * from shop_stats where date >= (NOW() - INTERVAL 12 MONTH) ORDER BY date

MySQL query - select entries not older than a time period

I have this query
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
The key part of my query is date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK). It returns me entries older than a week. What i want it to return me is entries NOT older than 1 week. How can i modify it to return me desired result?
Thank you.
Have you tried with
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date > DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
?

SQL selecting records with dates before today

I have a mysql DB with tables, of which in the one table I have a date type field, I want the most recently passed date - so I want it to order by dates descending, but only take records from before today, and then take only the top most one using the LIMIT function, and also there is the addition of the WHERE clause being that the offer must be for the selected city.
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]."
ORDER BY exp_date DESC
LIMIT 0, 1");
ADD another condition to where clause
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]." and Date < CURRENT_DATE()
ORDER BY exp_date DESC
LIMIT 1");
SELECT * FROM deals WHERE city = 2 AND exp_date < CURDATE()
ORDER BY exp_date DESC LIMIT 0, 1
Add the following condition to Where:
... and exp_date < CURDATE()
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html.

PHP / MySQL - Construct a SQL query

Im having a little trouble constructing a query.
I have a table with 3 columns.
id - day - pageviews
What i basically want to do is get 8 id's from the table where the pageviews are the highest from the last 60 days.
The day column is a datetime mysql type.
Any help would be great, im having a little trouble figuring this one out.
Cheers,
Almost the same as TuteC posted, but you'll need a group by to get what you need...
SELECT id, SUM(pageviews) totalViews
FROM table
WHERE DATE_SUB(CURDATE(), INTERVAL 60 DAY) <= day
GROUP BY id
ORDER BY totalViews DESC
LIMIT 8
Do something like this:
SELECT id FROM table_name
WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day
ORDER BY pageviews DESC
LIMIT 8;
$sixtyDaysAgo = date('Y-m-d',strtotime('-60 days'));
$sql = "SELECT id
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
ORDER BY pageviews DESC
LIMIT 8";
If each row is a number of pageviews for that day, and you're looking for the highest total sum of 60 days' worth, then you'll need to total them all and then grab the top 8 from among those totals, like so:
$sql = "SELECT id
FROM (
SELECT id, SUM(pageviews) AS total_pageviews
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
GROUP BY id
) AS subselect
ORDER BY total_pageviews DESC
LIMIT 8";

Categories