sql - MYSQL Query - When col1 has only 'Special' use that else use one of the other -


following dataset

  id  | description |   type ------------------------------- 12204 |     abc     | special 12204 |     def     | connector 12541 |     ghi     | special 12541 |     jkl     | special 12541 |     mno     | hybrid 13292 |     pqr     | resistor 13292 |     stu     | connector 13292 |     vwx     | hybrid 14011 |     yza     | special 14012 |     bcd     | resistor 

what want following:

  id  | description |   type ------------------------------- 12204 |     def     | connector 12541 |     mno     | hybrid 13292 |     pqr     | resistor or connector or hybrid <-- doesn't matter 14011 |     yza     | special 14012 |     bcd     | resistor 

so, need whole dataset according type. if there "special" type possible, need use it, if not want use 1 of others.

i figured out group uses first row contains special...

my query far:

select id, description, type anydatabase group id having count(type) > 1  

hope can :) adi

you can try this

select   l.id,   l.description,   if(r.type null, l.type, r.type) `type` newtable l   left join (select *          newtable          type <> 'special') r     on r.id = l.id group l.id 

sql fiddle demo


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -