Forum Moderators: open

Message Too Old, No Replies

MySQL SELECT CASE WHEN question

MySQL SELECT CASE WHEN

         

hja_nl

6:16 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



I have table called "categories_ctg" with the following fields:

id_ctg, int(11), auto_increment
idctg_ctg, int(11)(can be NULL)
name_ctg, varchar(100)
longname_ctg, varchar(255)
order_ctg, int(11)

The table is stored for categories, for instance
1, NULL, hardware, hardware, 1
2,1,computers,computer systems,2
3,1,printers,printers,3
4,NULL,software,software,4

I use this to navigate on a website. What I want is to show the above al follows:

hardware > computers
hardware > printers
software

The query I've tried is:
SELECT subcategories_ctg.id_ctg, CASE WHEN categories_ctg.name_ctg IS NULL THEN subcategories_ctg.name_ctg ELSE categories_ctg.name_ctg, ' > ', subcategories_ctg.name_ctg END as name_ctg
FROM categories_ctg, subcategories_ctg LEFT JOIN categories_ctg ON subcategories_ctg.idctg_ctg=categories_ctg.id_ctg
ORDER BY name_ctg

But this gives the following error:
"There is something wrong in the syntax at ' ' > ', subcategories_ctg.name_ctg END as name_ctg FROM categori' at line 1.

I am using DW8.02 to edit the queries. If anybody could tell me how to fix this, I'll be grateful, since I'm not a real specialist :).

Greetings,
Henk

physics

1:04 am on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Personally I would recommend you simplify the query by not trying to do everything in one lin, i.e. do the formatting in your PHP or Perl code (not sure what you're using for code).

zCat

1:09 am on Jun 23, 2006 (gmt 0)

10+ Year Member



I suspect the offending line needs to be written like this:

CONCAT(categories_ctg.name_ctg, ' > ', subcategories_ctg.name_ctg)