简体   繁体   中英

Mysql and case statement not working

/*-------------temp table--------------*/
CREATE TEMPORARY TABLE IF NOT EXISTS bowlpick.tempschedule AS

(SELECT * FROM bowlpick.schedule

where season = (select max(year) from bowlpick.season));
/*-------------temp table--------------*/

set @g1= (select winner from bowlpick.tempschedule where game =1);

select date,name, @g1,bowl1 Pick,comf1

,case 
    when bowl1 =  @g1 then  comf1 
    when bowl1 <> @g1 then -comf1 
else '0' end pts

from bowlpick.picks
where year(date) = (select max(year) from bowlpick.season)
order by bowl1 desc limit 5



<--------------------Table----------------->
  name      @g1            Pick       comf1 pts
Player 1    Boise State     Washington  7   -7
Player 2    Boise State     Boise State 30  -30
Player 3    Boise State     Boise State 21  -21
Player 4    Boise State     Boise State 27  -27
Player 5    Boise State     Boise State 15  -15

Why does this not work??? The last four pts should be positive numbers, not negative. It as if the variable (@g1) is not working in the 'CASE' part. It works correctly when the 'select' part. If I hard-code the winner (Boise State), it works correctly. Does variables not work in case statements?

Not sure why you are using temporary tables, you could do it in a single query, see the following example for somewhere to start, one other thing I noticed was that the case was returning numbers for the two case statements and a string for the else. Not only this the case statement has 2 WHEN statements which cover all bases surely meaning '0' would have never been selected anyway.

SELECT 
    bowlpick.picks.date
    ,bowlpick.picks.name
    ,bowlpick.tempschedule.winner
    ,bowlpick.picks.bowl1 Pick
    ,bowlpick.picks.comf1
    ,CASE 
        WHEN bowlpick.picks.bowl1 =  bowlpick.tempschedule.winner  THEN bowlpick.picks.comf1 
        WHEN bowlpick.picks.bowl1 <> bowlpick.tempschedule.winner  THEN -bowlpick.picks.comf1 
        ELSE 0
    END pts
FROM 
    bowlpick.picks
    LEFT JOIN bowlpick.tempschedule ON year(bowlpick.picks.date) = bowlpick.tempschedule.season 
WHERE 
    bowlpick.tempschedule.game =1
ORDER BY 
    bowl1 DESC
LIMIT 5

The following is an additional bit of work removing the need for the temporary table (links and grouping would need better defining but with an incomplete schema it's the best I could do)

SELECT 
    p.Date
    ,p.Name
    ,s.Winner
    ,p.Bowl1 Pick
    ,p.Comf1
    ,CASE 
        WHEN p.Bowl1 =  s.Winner  THEN p.Comf1 
        WHEN p.Bowl1 <> s.Winner  THEN -p.Comf1 
        ELSE 0
    END pts
FROM 
    picks p
    LEFT JOIN schedule s ON YEAR(p.Date) = s.Season 
WHERE 
    s.game = 1
GROUP BY 
    s.Season, p.Date
HAVING
    s.Season = MAX(s.Season)
ORDER BY 
    p.Bowl1 DESC
LIMIT 5

He's my SQL Fiddle used to do this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM