mysql - Is it possible to put an <if> Condition in a select Query -


mysql> select * categories ; +-------------+--------------------+-----------------+--------------------- | category_id | t1                 | t2              | t3 +-------------+--------------------+-----------------+--------------------- |           2 | popcorn            | bucket          | null |           3 | popcorn            | jumbo           | null |           3 | popcorn            | jumbo           | null             6 | popcorn            | combo relish    | null                |             7 | soft drinks        | fountain        | apple               |             7 | soft drinks        | fountain        | apple               |             8 | soft drinks        | fountain        | orange              |             8 | soft drinks        | bottled         | orange              | 

i have table called categories shown above .

for t2 values t3 can null or can have value .

i have got query shown below

select t1, t2,      group_concat(concat(t3,'(',category_id,')')) consildated_data  categories group 1,2; 

with results looks

popcorn            | bucket          | null popcorn            | combo relish    | null popcorn            | jumbo           | null soft drinks        | fountain        | apple(7),apple(7),orange(8) soft drinks        | bottled         | orange(8) 

my question if t3 null , can have value printed empty corresponding category_id number ??

so looke bucket

popcorn            | bucket          | empty(2) popcorn            | jumbo           | empty(3),empty(3) 

is possible put if condition under above query ??

without testing, can use if statement this:

select     t1,     t2,     if(         t3 null,                                  -- condition         concat('empty(', category_id, ')'),          -- true reaction         group_concat(concat(t3,'(',category_id,')')) -- false reaction     ) consildated_data categories group 1, 2; 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -