I have a date in this format.
08 april 1989
02 December 1984
13 January 1986
I would like to sort the results using month and day ,which has column "dateofb" and i sorting is like
13 january 1986
08 april 1989
02 december 1984
I have used the below code which doesn't work fine ,
$sel = $db->query("select * from biography where dateofb >= (CURDATE() - INTERVAL 90 DAY) order by dateofb desc limit 0,3");
I would like to display the 3 sorted results coming 90days.
Is it possible to convert the dateofb column to DATE column type? This would allow you to do what you're looking for. The format you have now is invalid and would need to be converted via STR_TO_DATE(dateofb, '%d %M %Y')
Example:
$sel = $db->query("select * from biography where STR_TO_DATE(dateofb, '%d %M %Y') >= (CURDATE() - INTERVAL 90 DAY) order by STR_TO_DATE(dateofb, '%d %M %Y') desc limit 0,3");
^- See how gross that looks? If your dateofb was a DATE column, you could just do:
$sel = $db->query("select * from biography where dateofb >= (CURDATE() - INTERVAL 90 DAY) order by dateofb desc limit 0,3");
^- which is your original query.
If your "dateofb" field is a DATE or DATETIME:
SELECT
*
FROM biography
WHERE dateofb >= (CURDATE() - INTERVAL 90 DAY)
ORDER BY MONTH(dateofb), DAY(dateofb) ASC
LIMIT 0, 3
Related
I am somewhat familiar with the MySQL DATE and DATE_FORMAT.
However, when running a query for a date range, using INTERVAL - 30 DAY, am either not getting the correct output or it throws an error. Looking further into the table, realize that each row for date_buy is in this type of format:
Mon 09 September 2015, etc. I have tried to use PHP date('D d F Y') instead of DATE(NOW()) and such, but am unable to resolve the issue. Is there another known workaround for this?
And yes, have tried a BETWEEN date_buy_x and date_buy_y. It returns nothing even though there are records in the table.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy >= $date - INTERVAL 30 DAY
GROUP BY date_buy
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE date_buy BETWEEN date_buy_x && date_buy_y
GROUP BY date_buy ORDER BY date_buy DESC
UPDATE
SO, based upon the suggestions, and looking further into the DATE_FORMAT and CURDATE parameters, have been able to display the data. The ORDER BY must also be in DATE_FORMAT(date_buy, '%a %d %M %Y') to output correctly. However, the issue still remains regarding INTERVAL 30 DAY parameter.
WHERE datebuy <= DATE_FORMAT(CURDATE() - INTERVAL 30 DAY,'%a %d %M %Y')
GROUP BY date_buy ORDER BY DATE_FORMAT(date_buy, '%a %d %M %Y') DESC
This is bringing back data but still way out of range, which makes me think it is not accepting the INTERVAL request.
Sat 22 November 2014 (50 sales)
Thu 18 December 2014 (50 sales)
Thu 22 January 2015 (20 sales)
Sun 25 January 2015 (20 sales)
Mon 06 April 2015 (25 sales)
Sun 12 April 2015 (25 sales)
Mon 03 August 2015 (10 sales)
When the output should only be:
Mon 03 August 2015 (10 sales)
You could conceivably do:
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= $date - INTERVAL 30 DAY
GROUP BY date_buy
Unless $date is also in this string format, in which case you need to use the STR_TO_DATE function on it too.
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE(STR_TO_DATE(date_buy, '%a %d %M %Y')) >= DATE(STR_TO_DATE($date, '%a %d %M %Y')) - INTERVAL 30 DAY
GROUP BY date_buy
how about date_sub(),use date_format to same format when query
SELECT SUM(sales_total), date_buy FROM `Sebastian Estate Sales`
WHERE DATE_FORMAT(DATE(date_buy,'%Y %m %d')) >= DATE_FORMAT(DATE_SUB(DATE($date,INTERVAL 30 DAY),'%Y %m %d'))
GROUP BY date_buy
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
So I have a table ("posts") where date is formatted as such (PHP syntax)
date = date("F j, Y")
and this is the date submitted in the posts table as well. How would I get all posts where date is younger than 7 days (i.e. current_date - 7) via an SQL query?
Any help appreciated
You need to convert that date string to an actual date MySQL can work with. You can use STR_TO_DATE() for that.
SELECT *
FROM posts
WHERE STR_TO_DATE(date_col, '%M %e, %Y') > CURRENT_DATE - INTERVAL 7 DAY
or
SELECT *
FROM posts
WHERE STR_TO_DATE(date_col, '%M %e, %Y') > DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.
I have a query that is grabbing the Date of a given post. I'm displaying this information in a chart that has the date and number of posts for that day. How can I display "Friday, Oct 15" instead of 2010-10-15??
Query:
$oct_week = mysql_query("SELECT COUNT(*), DATE(`dPostDateTime`) AS `day` FROM `tblQA` WHERE dPostDateTime >= '2010-10-01' AND dPostDateTime <= '2010-10-31' GROUP BY `day`");
DATE_FORMAT(DATE(`dPostDateTime`), '%W, %b, %e')