I want to search house_id
where all days appear within a range of 2 dates from startDate to finalDate.
Tables columns are:
- id
- house_id
- date
- status
house_id
+ date
are unique
the closest I could get is:
SELECT * FROM (
SELECT *
FROM tests) AS T1
WHERE date BETWEEN '2016-02-01' AND '2016-02-02'
HAVING COUNT(*) = DATEDIFF('2016-02-02', '2016-02-01') < 1
sample data:
CREATE TABLE IF NOT EXISTS `tests` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`),
`house_id` int(10) unsigned NOT NULL,
`date` date NOT NULL,
`status` enum('0','1','2') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `tests` (`id`, `house_id`, `date`, `status`) VALUES
(1,1, '2016-02-01', '1'),
(2,1, '2016-02-02', '1'),
(3,1, '2016-02-03', '1'),
(4,1, '2016-02-04', '1'),
(5,1, '2016-02-05', '1'),
(6,1, '2016-02-06', '1'),
(7,2, '2016-02-01', '2'),
(8,2, '2016-02-02', '2'),
(9,2, '2016-02-04', '2'),
(10,2, '2016-02-05', '2'),
(11,2, '2016-02-06', '2'),
(12,2, '2016-02-07', '2'),
(13,2, '2016-02-08', '2')
Best Answer
I believe you want to get
house_id
wheredate
exists for each day between@startDate
and@finalData
.Sample queries with 3 different
house_id
: SQL Fiddle.Query:
Query 1 in SQL Fiddle.
This query
GROUP BY house_id
and output theCOUNT
of days between@startDate
and@finalData
:From this query, it is obvious that only rows where
COUNT(*) = DATEDIFF(@startDate, @finalDate)
should be returned.If
(house_id, date)
is not unique,COUNT(*)
must be replaced byCOUNT(DISTINCT `date`)
.Query with all days
If you only want
house_id
whereCOUNT(*) = DATEDIFF(day, @startDate, @finalDate)
, the query must use theHAVING
clause:Query 2 in SQL Fiddle.
Output:
Query where
status = X
Status value can be checked in the
WHERE
clause:Query 3 in SQL Fiddle.
Output: