Jump to content

Extract and group by year


necken18

Recommended Posts

Hello.

 

I want to extract year and month from a datetime column in the database. And i want to display the results like this:

2008
Jan
May
2010
Oct
2011
Dec

 

But my code gets this result

 

2008
Jan
May
2010
Oct
2011
Dec

 

Here's my code.

	$get = $db->query("
	SELECT EXTRACT(YEAR FROM date) year, EXTRACT(MONTH FROM date) month
	FROM test
	GROUP BY 
		year
");

foreach ($get as $row)
{
	echo $row['year']."<br/>";
	echo $row['month']."<br/>";
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/225170-extract-and-group-by-year/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.