0

i have an events table having start date and end date I am trying retrieve all the records by giving a date that is between start and end dates.

eg :

SELECT * 
FROM   `events` 
WHERE  '2017-01-29' BETWEEN start_date='2017-01-28' 
                    AND     end_date='2017-01-31' 

but response is syntax error can any one help me to finish the query

4 Answers 4

2

Just list the columns.

WHERE '2017-01-29' BETWEEN start_date AND end_date

The values come from the table, you don't put them into the query.

Sign up to request clarification or add additional context in comments.

Comments

2

According to mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) the syntax for BETWEN is

expr BETWEEN min AND max

it is not

expr BETWEEN blabla=min AND stuff=max

Also, it is rather pointless to be using constants in all three expressions, because in this case the result will be known in advance (either always TRUE or always FALSE) without having to consult the values in your table.

It is kind of hard to give you an example without knowing the structure of your table, but what you probably want is something like

WHERE '2017-01-29' BETWEEN start_date
                   AND     end_date

(assuming start_date and end_date are columns in your table)

or something like

WHERE  some_column BETWEEN '2017-01-28' 
                   AND    '2017-01-31' 

(assuming some_column is a column in your table.)

2 Comments

can u give any suggestion to finish my requirment
@sivasandeepgarapati Doesn't my answer do that?
1

I believe you're trying to find all the rows where a date is 2017-01-29, and so, your query could be:

SELECT * 
FROM   `events` 
WHERE   
date = '2017-01-29';

If, however, you want all rows with date between 2017-01-28 and 2017-01-31, then you could do:

SELECT * 
FROM   `events` 
WHERE   
date BETWEEN '2017-01-28' AND '2017-01-31';

Comments

0

Instead of putting 2017-01-29 before WHERE, put the name of the field you want to filter by date, such as EventDate (or whatever your field is named).

1 Comment

I think he wants all events where that date is between the start and end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.