php - mysql query search record in BETWEEN and list if exits or not -
hi trying make query run records table , between tow years working fine need list values search between
query
select sum(price) orders year between 2004 , 2009 group year 2005 | 200 2006 | 100 2008 | 400 trying show 2005 | 200 2006 | 100 2007 | 0 2008 | 400 2009 | 0 -- ---------------------------- -- table structure `orders` -- ---------------------------- drop table if exists `orders`; create table `orders` ( `id` int(11) not null auto_increment, `price` decimal(10,0) default null, `year` int(11) default null, primary key (`id`) ) engine=myisam auto_increment=4 default charset=latin1; -- ---------------------------- -- records of orders -- ---------------------------- insert orders values ('1', '200', '2005'); insert orders values ('2', '100', '2006'); insert orders values ('3', '400', '2008');
or need in php loop or somthing suggestions
you need auxiliary tables of numbers can outer join on. in absence of permanent 1 use derived table.
select coalesce(sum(o.price),0) price (select 2004 yr union select 2005 union select 2006 union select 2007 union select 2008 union select 2009 ) y left join orders o on y.yr = o.year group y.yr
Comments
Post a Comment