开发者

Mysql: How to use CASE in query

开发者 https://www.devze.com 2023-03-09 12:09 出处:网络
I have following query : SELECT t.id as tid , t.uid tuid , t.bid_type , tt.type as type , t.bid_category , tc.type as category

I have following query :

SELECT
    t.id as tid
  , t.uid tuid
  , t.bid_type
  , tt.type as type
  , t.bid_category
  , tc.type as category
  , t.name, if(t.description IS NULL
  , '-', t.description) as description
  , date_format(t.sdate, '%m-%d-%Y %H:%i') as tsdate
  , date_format(t.endate, '%m-%d-%Y %H:开发者_运维技巧%i') as tendate
  , date_format(t.adate, '%m-%d-%Y %H:%i:%s') as tadate
  , if(t.edate IS NULL, '-', date_format(t.edate, '%m-%d-%Y %H:%i:%s')) as tedate
  , t.status
  , tq.id as tqid
  , tq.bid
  , if(tq.amount IS NULL, '-', tq.amount) as amount
  , if(tq.hand IS NULL, '-', tq.hand) as hand
  , if(tq.game = 0 OR tq.game IS NULL, '-', tg.type) as game
  , if(tq.stake = 0 OR tq.stake IS NULL, '-', ts.type) as stak
  , if(tq.player = 0 OR tq.player IS NULL, '-', tq.player) as player
  , if(tq.tourney IS NULL, '-', tq.tourney) as tourney
  , if(tq.propbet, 'Prop bet', '-') as propbet
  , if(tq.race, 'Race', '-') as race
  , tq.status as tqstatus 
  , CASE 
    WHEN tq.amount IS NOT NULL THEN tq.amount 
    WHEN tq.hand IS NOT NULL THEN tq.hand 
    WHEN tq.tourney IS NOT NULL THEN tq.tourney 
    WHEN tq.propbet IS NOT NULL THEN 'Prop bet' 
    WHEN tq.race IS NOT NULL THEN 'Race' 
    END CASE as bidData
FROM (bid t)
JOIN bid_quote tq ON tq.bid = t.id
JOIN bid_type tt ON tt.id = t.bid_type
                AND tt.pid = 0
JOIN bid_type tc ON tc.id = t.bid_category
JOIN bid_game tg ON tg.id = tq.game
JOIN bid_stake ts ON ts.id = tq.stake

Above mentioned query is giving errors. Can some one guide me how to rectify it so CASE should work in query. Here's the error:

SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE as bidData FROM (bid t) JOIN bid_quote tq ON tq.bid = t.id JOIN bid_' at line 9


Remove the word CASE in END CASE. In a query you end a case with END alone, in contrast to stored procedures.

PS: You know you're allowed to use enters and indentation in queries too, right? ;-)


There is case for Stored Procs and one for queries.

http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#operator_case

0

精彩评论

暂无评论...
验证码 换一张
取 消