php - SQL Query multiple where clause prioritize -
i have following sql query
select * train_stop code='abc' or code='xyz' or code='def' group number order departs
now since using group by
take 1 row specific number. want prioritize result. means if table has code='abc' , code='xyz' , result must select abc
row , not 'xyz' because abc has come first in clause. if table has xyz
, def
same number, xyz
must come in result , not def
. till experiments led me hows on random basis. helpful if guide me achieve , proper indexing strategy case. thanks.
the create statement follows-
create statement `create table `train_stop` ( `number` varchar(1000) not null, `stop_number` int(11) not null, `code` varchar(1000) not null, `station name` varchar(1000) not null, `arrives` time not null, `departs` time not null, `halt` varchar(1000) not null, `pf` varchar(1000) not null, `day` int(11) not null, `km` varchar(1000) not null, `speed` varchar(1000) not null, `elev` varchar(1000) not null, `zone` varchar(1000) not null, `address` varchar(1000) not null, `active` int(11) default '1', key `index_1` (`number`(767),`code`(767)), key `pindex` (`number`(767),`stop_number`), key `three_columns_idx` (`code`(767),`active`,`departs`), key `two_columns_idx` (`code`(767),`active`), key `two_columns_group_idx` (`number`(767),`departs`), key `one_columns_group_idx` (`departs`) ) engine=innodb default charset=latin1`
group wrong way this. it. need change order by
portion match where
clause ordering whenever change codes check against.
select * ( select if(@prev = number, @rank := @rank + 1, @rank := 1 , @prev := number) rank, train_stop.* train_stop, (select @prev := 0, @rank := 1) q code = 'abc' or code = 'xyz' or code = 'def' order number asc, code='abc' desc, code='xyz' desc, code='abc' desc ) q rank = 1;
demo here: http://sqlfiddle.com/#!9/ccc4d/7
this query assigns rank per ordered group, , selects top ranked value out of each group. break if have station number 0
Comments
Post a Comment