Jshee
Jshee

Reputation: 2686

Select a subset of mysql database column - date

I have a query:

select post_name, post_title, post_date from mysite.wp_posts where post_status='publish'

post_date outputs a GMT date and time like: 2016-12-17 17:07:26

I want to select just 2016 and just 12 via the mysql query and not the rest.

How can i modify my query to do this?

Upvotes: 2

Views: 304

Answers (1)

Gurwinder Singh
Gurwinder Singh

Reputation: 39477

If you want them in separate columns:

SELECT post_name,
  post_title,
  YEAR(post_date) YEAR,
  MONTH(post_date) MONTH
FROM mysite.wp_posts
WHERE post_status='publish';

If in same column like 2016-12:

SELECT post_name,
  post_title,
  date_format(post_date, '%Y-%m') year_month
FROM mysite.wp_posts
WHERE post_status='publish';

Upvotes: 1

Related Questions