sql - MySQL Group By And Skip Grouping On Null Values -


select * dc_deal group collection_id 

in collection_id column have values (1,3,3,4,4,5,null,null). above query return rows (1,2,3,4,null) want skip grouping on null value , need result (1,2,3,4,null,null)

if have unique column (or set of columns) in table, can add expression group by. expression needs return unique value each row when collection_id null, otherwise, returns constant.

assuming have unique id column in table, this:

... group collection_id, if(collection_id null, id, 0) 

that second expression in group returns constant value when collection_id not null, returns unique value each row when collection_id null. (note id here reference column defined unique within table, primary key candidate. if don't have unique index on single column, can repeat type of expression each column in unique constraint, or set of expressions guaranteed unique on each row.

... group collection_id            , if(collection_id null, col1, '')            , if(collection_id null, col2, null)            , if(collection_id null, col3, collection_id) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -