How to select between dates

Asked

Viewed 10,959 times

3

My question is quite simple, I intend to ask a SELECT between dates, from a given table, where the status='1'.

I mean, I have for example:

$data1 = 13/01/2017
$data2 = 20/01/2017

I want to select all results between these dates where the status=1

How can I do?

2 answers

3

You can use multiple filters on WHERE, just put AND among them.

For an interval between two dates, use BETWEEN.

Remember that dates need to be in quotes and in yyyy-mm-dd format.

Example below:

SELECT *
FROM TABELA
WHERE 
    status='1'
    and data between '2017-01-13' and '2017-01-20';

Note, this if your database field is set to DATE.

If you are on DATETIME, you have to use '00:00:00' for the first date '23:59:59' for the second.

SELECT *
FROM TABELA
WHERE 
    status='1'
    and data between '2017-01-13 00:00:00' and '2017-01-20 23:59:59';

If it is in DATETIME format and you don’t want to worry about putting the time (suggestion by @Bacco):

SELECT *
FROM TABELA
WHERE 
    status='1'
    and DATE( data ) between '2017-01-13' and '2017-01-20';

If it is in TIMESTAMP format, you can use UNIX_TIMESTAMP as suggested by @Inkeliz.

  • @Bacco, could you help me with a question? http://answall.com/q/177343/31016 At least give me a path... if you can chat, let me know. Or send me a text in the comment.

  • I left a tip there, I’m on my way out, but if you leave a test in the codepen, it’ll help us try something. Even if I can’t see it today, some colleague might see the link and comment with the hint, and try to help you. I think this one of doing the Chosen separate key event will help. Then in the IF you see if the key is del, and if the element is Chosen. If it is not leave the event quiet pro browser do what you would normally do. if it is, call the method you want. Ai does not even touch the source of Chosen.

  • I tried Chosen quickly here, and delete erases Abels, but in the fields with text to type delete works normally to delete selection. If you can, then leave a comment here or there later explaining what goes wrong when you try to delete. I tested on FF 50.1.0 on Windows 7

3

It depends on the format that is in your database.

If it is in format DATETIME:

Use DATE():

SELECT * FROM Tabela WHERE Status = 1 AND DATE(Data) > '2017-01-01' AND DATE(Data) < '2017-01-17'

Use 00:00:00:

SELECT * FROM Tabela WHERE Status = 1 AND Data > '2017-01-01 00:00:00' AND Data < '2017-01-17 00:00:00'

If it is in format DATE:

SELECT * FROM Tabela WHERE Status = 1 AND Data > '2017-01-01' AND Data < '2017-01-17'

If it is in format TIMESTAMP/INT:

Use UNIX_TIMESTAMP():

SELECT * FROM Tabela WHERE Status = 1 AND Data > UNIX_TIMESTAMP('2017-01-01') AND Data < UNIX_TIMESTAMP('2017-01-17')
  • In the case of date, the date format is y-m-d?

  • 1

    Exactly, this is the standard of Mysql.

  • Date is yyyy-mm-dd. Anything other than that is not date on DB. If you need to convert to store correctly, you have this: http://answall.com/questions/17679/70 - which is one of the simplest ways to keep correct in DB but show and write formatted in dd mm yyyy;

  • I will try, here in my project, as soon as I finish validating the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.