Question

Table clients

ID     |   Name    | start_Date          |  Future
1          John      20-02-2014 15:00:00     0
2          Maria     20-02-2014 15:00:00     0
3          Stack     21-02-2014 15:00:00     1
4          Google    21-02-2014 15:00:00     0

Currently, my query looks like this:

SELECT * FROM clients

Obviously this will get all clients, but I want to display the clients according to the Future variable. When Future variable is set the clients can only show from the start_Date to further.

If I change my query to this:

SELECT * 
FROM clients
WHERE (NOW() >= start_Date OR start_Date IS NULL
       OR start_Date = '0000-00-00 00:00:00') 

This will get me:

ID     |   Name    | start_Date          |  Future
1          John      20-02-2014 15:00:00      0
2          Maria     20-02-2014 15:00:00      0

But the output I expect is:

ID     |   Name    | start_Date          |  Future
1          John      20-02-2014 15:00:00     0
2          Maria     20-02-2014 15:00:00     0
4          Google    21-02-2014 15:00:00     0

I don't want to show

3          Stack     21-02-2014 15:00:00      1

because the variable Future is set to 1 and it can only be shown tomorrow (21-02-2014 15:00:00). So, finally, I've come up with this without success:

SELECT * 
FROM clients
WHERE CASE WHEN Future = 1 THEN (NOW() >= start_Date OR start_Date IS NULL
       OR start_Date = '0000-00-00 00:00:00') END 
Was it helpful?

Solution

Try this:

SELECT * 
FROM clients
WHERE future = 0
OR (future = 1 
    AND (NOW() >= start_Date 
         OR start_Date IS NULL 
         OR start_Date = '0000-00-00 00:00:00')) 

OTHER TIPS

why not use this

 SELECT * 
 FROM clients
 WHERE future = 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top