mysql - How to put multiple if conditions dynamically in select statement -


i have table called categories shown below.

mysql> select * categories; +-------------+--------------------+-----------------+---------------------+------+------+------+------+------+ | category_id | t1                 | t2              | t3                  | t4   | t5   | t6   | t7   | t8   | +-------------+--------------------+-----------------+---------------------+------+------+------+------+------+ |           1 | popcorn            | regular pack    | null                | null | null | null | null | null | |           2 | ice creams         | cone            | koolcool            | choclate | null | null | null | null  |           3 | softdrinks         | pepsi           | tin                 | large | lemon | null | null | null |  |           3 | softdrinks         | coke            | tin                 | large | apple | mix | null | null |  

i have got different categories of items mentioned inside t1 column (popcorn, ice creams , softdrinks) , each category can have t8 levels

with below query supporting upto t3 level

select t1,        if( t3 null, concat('(', category_id, ')'), group_concat(concat(t3,'(',category_id,')'))) consildated_data categories group 1; 

but right need support t8 levels

could please tell me how achieve

say example if popcorn ends @ t3 level needed t2 level , category_id in bracket .

+--------------------+-----------------+---------------------------------------------------------+ | t1                                  | consildated_data                                        | +-------------------------------------+---------------------------------------------------------+ | popcorn                             | regular pack(1)                                                | | soft drinks                         | pepsi,tin,large,lemon(3)      | soft drinks                         | coke,tin,large,apple,mix(3)                                  |   ice creams                             cone,koolcool,choclate(2)  --------------------+-----------------+---------------------------------------------------------+ 

this sqlfiddle

http://sqlfiddle.com/#!2/0cfe8/1

i'd return specified resultset this:

select c.t1      , concat(          trim(trailing ','             concat_ws(','              ,ifnull(c.t2,'')              ,ifnull(c.t3,'')              ,ifnull(c.t4,'')              ,ifnull(c.t5,'')              ,ifnull(c.t6,'')              ,ifnull(c.t7,'')              ,ifnull(c.t8,'')            )          )            ,'('          ,c.category_id          ,')'        ) consolidated_data   categories c 

just concatenate t1 thru t8 comma separator; remove trailing commas that, , tack on category_id in parens.

given sample data, group by clause isn't going work, eliminate rows, , you'd 1 row category_id (3).


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -