Question: This is a Mysql/sql query question but allow me to explain the context:
I would like to equip my PHP calendar with a '< PREV' and 'NEXT >' button allowing a user to easily go to the previous or next available month **that has DATABASE ENTRIES AVAILABLE**. If there are no database entries available, I want to disable the button (I never want the system to present an empty calendar).

**Example:**
User has FEB in view. JAN and MAR do not contain entries but DEC and APR do. The 'NEXT >' button then needs to switch to the April calendar. The '< PREV' button needs to switch to the December calendar.

**So basically I'm looking for a single efficient query that can check if entries exist prior to and after (but not part of) the month in view.**

The table has a field 'logDate' which is of type date 'Y-m-d'.

I started fumbling with the following query to check first whether or not a date exists before and after the given date but I understand that there are some things wrong with this query. For one, if there's an entry in the table for '2013-03-19' my query will spit out a 1 for prev - which of course doesn't indicate if an entry exists for months prior to March.

```
SELECT IF( EXISTS(SELECT dayId FROM LogbookDay 
WHERE logDate < '2013-03-20' AND userId = 2), 1, 0) as prev, 
IF( EXISTS(SELECT dayId FROM LogBookDay WHERE logDate > '2013-03-20' 
AND userId = 2), 1, 0) as next

```

Answer: 
With my own query and a pointer from one of the posted answers I have been able to come up with a simple query that checks whether or not entries exist with months prior and after to the month currently in view.

Assuming month in view is March 2013:

```
SELECT IF( EXISTS(SELECT dayId FROM LogBookDay 
WHERE logDate < '2012-03-01' AND userId = 1), 1, 0) as prev,
IF( EXISTS(SELECT dayId FROM LogBookDay 
WHERE logDate > CONCAT('2012-03-',LAST_DAY('2003-02-05')) 
AND userId = 1), 1, 0) as next

```
```
SELECT 
if(min(logDate) < '2013-03-20', 1, 0) 'NEXT',
if(max(logDate) > '2013-03-20', 1, 0) 'PAST'
FROM LogbookDay 
WHERE logDate < '2013-03-20' AND logDate > '2013-03-02' 
AND  userId = 2

```