Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Good tutorial for CASE? 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi,

Can't find a good tutorial about the CASE function.
Google isn't my friend anymore :(
 
You must be kidding? ;)

What I need is a good tutorial with lots of practical examples of queries.

It's after I saw this MISERABLE official documentation that I posted here.
 
nope, wasn't kidding

i guess the mysql docs are an acquired taste, like snails or sushi, some people just can't ever get to like them

you sound like an intelligent guy, so i hesitate to suggest that you google for what you want...

try a couple o' these (remember, as far as syntax is concerned, all databases implement CASE similarly) --

(modest fee required)

r937.com | rudy.ca
 

I see.
I googled with "mysql" instead of just "sql" :(

As for the documentation, it's not about taste.
The explanation dedicated to CASE is way shorter that any link you have found. They show you the basic syntax and then you're on your own.
 
I googled with "mysql" instead of just "sql" :(

remember that mysql is just one "flavour" of sql. personally it is why I stay away from proprietary items and stick to the standard SQL, for the most part they can be easily used in other databases.
 
Yay! I've finally writen my first successful CAUSE statement!

Code:
SELECT table_ID, service_type_ID, service_order, 
        CASE 
        WHEN service_type_ID IS NULL THEN CONCAT(table_ID, 0)
	ELSE CONCAT(service_type_ID, service_order)
        END AS final_order
FROM mod_hotel_services
ORDER BY final_order
( )

BTW, r937, you're a liar!
I've been to Toronto and I swear that it's not true that "the weather allows you to play frisbee golf all year long." :)
Brrr what a dull and freezing place it was ...
 

The only living creatures I saw outside were frozen squirels.
I prefer much sexier spectators when I play :)
And no skirt = no fun.
 
Hmmm I don't get it.
Why can you use a constructed column on an ORDER clause but not on a WHERE clause???

For example, this doesn't work :

Code:
SELECT
table_ID, service_type_ID, service_order,

        CASE
        WHEN service_type_ID IS NULL THEN CONCAT(table_ID, 0)
	ELSE CONCAT(service_type_ID, service_order)
        END AS final_order,

        CASE
        WHEN service_type_ID IS NULL THEN table_ID
	ELSE service_type_ID
        END AS final_where

FROM
mod_hotel_services
WHERE
final_where = "1"
ORDER BY
final_order
 
how come you can't? because you can't!

:)

the reason has to do with the sequence in which the clauses are executed:

FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY

r937.com | rudy.ca
 
Did it ever happen that you admit the mysql team design things the wrong way? :)

I'm sure you'll find a good reason (one that I can't understand) for the weird order in which the clauses are executed butin the end, it doesn't do something that should be feasible.

Well, I guess nothing can be perfect ( just like Toronto ) :)
 
if you want to use the column aliases in the WHERE clause, you have to do this:
Code:
[blue]SELECT *
  FROM ([/blue]
       SELECT table_ID
            , service_type_ID
            , service_order
            , CASE
                WHEN service_type_ID IS NULL 
                THEN CONCAT(table_ID, 0)
                ELSE CONCAT(service_type_ID, service_order)
               END AS final_order
            , CASE
                WHEN service_type_ID IS NULL 
                THEN table_ID
                ELSE service_type_ID
               END AS final_where
         FROM mod_hotel_services
       [blue]) as data
 WHERE final_where = 1
ORDER 
    BY final_order[/blue]
one of the very few instances where i think it's okay to use "select star"

by the way, your second CASE can be simplified as follows:

COALESCE(service_type_ID,table_type_ID) as final_where

r937.com | rudy.ca
 
Wow ... that's the weirdest query I've seen so far!
It's so counter intuitive!
 
I have another problem :(

The same table again :

Code:
+----------+-----------------+----------------------------------+---------------
| table_ID | service_type_ID | service_type_name                | service_order
+----------+-----------------+----------------------------------+---------------
|        1 |            NULL | service 1                        | 1
|        2 |            NULL | service 2                        | 2
|        4 |               1 | NULL                             | 1
|        5 |               1 | NULL                             | 2
|        6 |               1 | NULL                             | 3
|        7 |               2 | NULL                             | 1
|        8 |               2 | NULL                             | 2
+----------+-----------------+----------------------------------+---------------

What if I want to display the rows of table_ID IN (5,6) plus the row that has table_ID equal to the service_type_ID of 5 and 6 by using CASE directly INSIDE the where clause? Is it possible?

Code:
SELECT table_ID, service_type_ID, service_order,

     CASE
     WHEN service_type_ID IS NULL THEN CONCAT(table_ID, 0)
     ELSE CONCAT(service_type_ID, service_order)
     END AS final_order

FROM mod_hotel_services
WHERE

     CASE ... ??? 
     END

ORDER BY final_order

The result is then supposed to be :

Code:
+----------+-----------------+----------------------------------+---------------
| table_ID | service_type_ID | service_type_name                | service_order
+----------+-----------------+----------------------------------+---------------
|        1 |            NULL | service 1                        | 1
|        5 |               1 | NULL                             | 2
|        6 |               1 | NULL                             | 3
+----------+-----------------+----------------------------------+---------------

Thanks x 1000 :)
 
use two selects and union them together

one for id in (5,6), the other for a self-join of service_type_id equal to table_id

r937.com | rudy.ca
 
Thanks!
I thought CASE could simplify everything but apparently it's not the ... case ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top