[Solved] SQl : Get all rows if date is nul
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

SQl : Get all rows if date is nul

Database type:

I am writing a php function in which i am taking 2 parameters ,start_date and end_date

I also want if both dates are Null then this function should return the all rows

Here is my mysql query

select COUNT(*) from demo_meeting where meeting_datetime > "2013-01-01 OR NULL" and  meeting_datetime < "2013-09-10 OR NULL";

THis query is working properly if both dates are available if i am using both date as NULL it is returning the Zero rows .

PLease tell me how can i improve this query so that if both date are NUll then it could return the all meetings


How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `demo_meeting` ADD INDEX `demo_meeting_idx_meeting_datetime` (`meeting_datetime`);
The optimized query:
        demo_meeting.meeting_datetime > '2013-01-01 OR NULL' 
        AND demo_meeting.meeting_datetime < '2013-09-10 OR NULL'

Related Articles

* original question posted on StackOverflow here.