RGS
RGS

Reputation: 4253

select where year less than 2014

I want to select all posts from 2014 or less. How can I do this?

SELECT id, fotos, cover, titulo, descricao, data, link_key FROM posts where `date` < '2014'

data is datetime.

Upvotes: 0

Views: 464

Answers (3)

Priyansh Goel
Priyansh Goel

Reputation: 2660

You can use extract function.

SELECT id,
       fotos,
       cover,
       titulo,
       descricao,
       data,
       link_key
FROM posts
WHERE EXTRACT(YEAR FROM date) < 2014

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521629

You could also use the YEAR function:

SELECT id,
       fotos,
       cover,
       titulo,
       descricao,
       data,
       link_key
FROM posts
WHERE YEAR(date) < 2014

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270081

I would suggestion:

where date < '2014-01-01'

Keeping the comparison simple ensures that indexes and partitions can be used for the query.

Upvotes: 5

Related Questions