I want to sort clanwars by date, without showing the clanwars that are before today (showing the clanwars of today too, ">="), but I don't know how, I'm using webspell (CMS) and don't know how to do it. This is the code:
$ergebnis=safe_query("SELECT * FROM ".PREFIX."clanwars ORDER BY date ASC LIMIT 0, ".$maxresults);
Any help will be appreciated.
$ergebnis=safe_query(
"SELECT *
FROM ".PREFIX."clanwars
WHERE `date` > DATE(NOW())
ORDER BY `date` ASC LIMIT 0, ".$maxresults);
NOW() returns the current date and time, DATE() returns just the date part, which is equivalent to 2013-09-12 00:00:00.
Try this:
$ergebnis=safe_query("SELECT * FROM ".PREFIX."clanwars WHERE date >= curdate() ORDER BY date ASC LIMIT 0, ".$maxresults);
Edit: convert date field to MySQL date format
$ergebnis=safe_query("SELECT * FROM ".PREFIX."clanwars WHERE STR_TO_DATE(date, '%d.%m.%Y') >= curdate() ORDER BY date ASC LIMIT 0, ".$maxresults);
Related
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;
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
I am creating a website that lists upcoming events, events are saved in a DB.
I am trying to only show events that are happening today or in the future, not in the past
here is my code:
("SELECT * FROM events where deleted='0' AND date > CURDATE() ORDER by STR_TO_DATE(date, '%d/%m/%Y') ASC ")
However this doesn't seem to show anything at all, can anyone help?
Thanks
The field date seems to be in the '%d/%m/%Y' format and is not defined as date, datetime, or timestamp data type.
Change:
SELECT * FROM events
where deleted='0'
AND date > CURDATE()
ORDER by STR_TO_DATE(date, '%d/%m/%Y') ASC
To:
SELECT * FROM events
where deleted='0'
AND STR_TO_DATE( date, '%d/%m/%Y' ) >= CURDATE()
ORDER by
STR_TO_DATE( date, '%d/%m/%Y' ) ASC
I have the following dates in my table. How do I find a closest date from either today (if today's date is there) or if today's date is not there then the nearest past date?
2012-10-01 aa123
2012-10-02 aa43
2012-10-03 aa478
2012-10-04 aa40
2012-10-05 aa54
2012-10-06 de34
2012-10-07 a5434
2012-10-08 r4t
2012-10-09 x34
2012-10-10 q23
2012-10-11 b53
So if today is '2012-10-07' is then the record will be a5434. But if 2012-10-07 is missing then the record will be de34 which belongs to 2012-10-06 since that would be the closest past day from today.
I am not sure where to start on this one, so I haven't tried anything yet. Need a sql solution to this.
It's simple, just get one of the last date <= the current date:
$now = date("Y-m-d");
$sql = "SELECT * FROM date_table where date_field <= '$now' ORDER BY date_field DESC LIMIT 1 OFFSET 1";
Add an ORDER BY statement to the query. The following will order the rows by their date, with the latest at the top and oldest at the bottom.
SELECT `id`, `date` FROM `table` ORDER BY `date` DESC LIMIT 1;
I have a problem with a query (mysql).
I have to order by date ASC tha problem is that I also have empty date in the table. Mysql set them to 0000-00-00, so when I order by date those dates are shown first, but i need to put the "empty dates" at the end, how can I do?
Example:
0000-00-00
2011-01-01
2010-12-12
Query: .... ORDER BY date ASC
Results should be:
2010-12-12
2011-01-01
0000-00-00
Thanks
ORDER BY IF(date = '0000-00-00', 1, 0) ASC, date ASC
Assuming you're doing:
SELECT *
FROM table
ORDER BY date ASC
you could do
SELECT *, IF(date > 0, 1, 0) AS has_date
FROM table
ORDER BY has_date ASC, date ASC
I believe that would do it for you.