Search This Blog

Tuesday, 20 August 2013

Search the date range between two dates SQL



CREATE TABLE `product_sales` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `From_date` date NOT NULL,
  `To_date` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `Product_sales`
--

INSERT INTO `Product_sales` (`id`, `name`, `From_date`, `To_date`) VALUES
(1, 'Product 1', '2013-08-20', '2013-08-30'),
(2, 'Product 2', '2013-08-14', '2013-08-27'),
(3, 'Product 3', '2013-08-25', '2013-08-31'),
(4, 'Product 4', '2013-08-10', '2013-08-22'),
(5, 'Product 4', '2013-08-24', '2013-08-28'),
(6, 'Product 4', '2013-08-23', '2013-08-30');

QUERY:
SELECT * FROM Product_sales WHERE ( From_date >= '2013-08-19' AND To_date <= '2013-08-23' ) OR ( To_date >= '2013-08-19' AND From_date <= '2013-08-23' )

No comments:

Post a Comment