sql - MySQL limit by count and return items with same date -


assuming have following table structure

create table `calendar` (   `id` int(11) not null auto_increment,   `title` varchar(255) not null,   `date` date not null,   primary key (`id`) ) 

and following data

insert `calendar` (`title`, `date`) values ('day 1 - event 1', '2013-05-01'), ('day 2 - event 1', '2013-05-02'), ('day 2 - event 2', '2013-05-02'), ('day 3 - event 1', '2013-05-03'); 

i hoping limit result set 2 items not cut result in between items of same date.

select * `calendar` `date` >= '2013-05-01' limit 2 

yield

('day 1 - event 1', '2013-05-01'), ('day 2 - event 1', '2013-05-02'), ('day 2 - event 2', '2013-05-02') 

instead of just

('day 1 - event 1', '2013-05-01'), ('day 2 - event 1', '2013-05-02') 

any ideas?

something should work putting desired dates in subquery:

select distinct c.*  `calendar` c   join (        select `date`        `calendar`        `date` >= '2013-05-01'        limit 2     ) c2 on c.`date` = c2.`date` 

sql fiddle demo

i recommend adding order by date query though can't guarantee order of results without.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -