Yesterday, a member of stackoverflow helped me change a table in my database with a case from another table. But I need to input one more condition, and don't know where to put it. I have theese two tables:
liga
uid | name | games | points |
___________________________________
1 | Daniel | 0 | 0 |
2 | Mikkel | 0 | 0 |
and kamp2:
uid | k1 | k1r | k2 | k2r | week |
__________________________________________________
1 | 1 | 2-1 | X | 2-2 | 14 |
2 | 2 | 1-1 | 1 | 2-1 | 14 |
To those who haven't got it yet, it's a betting-game, where the user bets on 2 matches (k1 and k2) and a result on both (k1r and k2r). Let's say the first match is 2-1, i use:
UPDATE liga l JOIN
kamp2 k
ON l.uid = k.uid
SET point = (CASE WHEN k.k1 = '1' and k.k1r = '2-1' THEN point + 5
WHEN k.k1r = '2-1' AND k.k1 <> '1' THEN point + 3
WHEN k.k1 = '1' AND k.k1r <> '2-1' THEN point + 1
ELSE point
END)
WHERE l.uid = k.uid ;
and it workes perfectly. Daniel gets 5 points and Mikkel gets 3 points. But now to my problem. Next week they will send in another bet with the week-number 15. So I actually just want it to
UPDATE liga l JOIN
kamp2 WHERE k.week = 15
......
but it won't allow that. Where should I put the condition then?
You should add the where clause to the end (where the other where
clause is located).
In sql you first state which tables you like to update UPDATE table
, then which fields you want to update with what values SET field='val'
and lastly on which records this should be done WHERE field=42
.
UPDATE liga l JOIN kamp2 k ON l.uid = k.uid
SET point = (CASE WHEN k.k1 = '1' and k.k1r = '2-1' THEN point + 5 WHEN k.k1r = '2-1'
AND k.k1 <> '1' THEN point + 3 WHEN k.k1 = '1'
AND k.k1r <> '2-1' THEN point + 1 ELSE point END)
WHERE l.uid = k.uid AND k.week=15 ;
I don't know whether I understood your question,but you can use
l.uid = k.uid and k.week = 15
or
where(case1,case2)
The last method is own mysql.