6
drop table if exists t1, t2;
9
select CASE "b" when "a" then 1 when "b" then 2 END;
10
select CASE "c" when "a" then 1 when "b" then 2 END;
11
select CASE "c" when "a" then 1 when "b" then 2 ELSE 3 END;
12
select CASE BINARY "b" when "a" then 1 when "B" then 2 WHEN "b" then "ok" END;
13
select CASE "b" when "a" then 1 when binary "B" then 2 WHEN "b" then "ok" END;
14
select CASE concat("a","b") when concat("ab","") then "a" when "b" then "b" end;
15
select CASE when 1=0 then "true" else "false" END;
16
select CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END;
17
explain extended select CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END;
18
select CASE 2.0 when 1 then "one" WHEN 2.0 then "two" ELSE "more" END;
19
select (CASE "two" when "one" then "1" WHEN "two" then "2" END) | 0;
20
select (CASE "two" when "one" then 1.00 WHEN "two" then 2.00 END) +0.0;
21
select case 1/0 when "a" then "true" else "false" END;
22
select case 1/0 when "a" then "true" END;
23
select (case 1/0 when "a" then "true" END) | 0;
24
select (case 1/0 when "a" then "true" END) + 0.0;
25
select case when 1>0 then "TRUE" else "FALSE" END;
26
select case when 1<0 then "TRUE" else "FALSE" END;
29
# Test bug when using GROUP BY on CASE
31
create table t1 (a int);
32
insert into t1 values(1),(2),(3),(4);
33
select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
34
explain extended select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
35
select case a when 1 then "one" when 2 then "two" else "nothing" end as fcase, count(*) from t1 group by fcase;
39
# Test MAX(CASE ... ) that can return null
42
create table t1 (row int not null, col int not null, val varchar(255) not null);
43
insert into t1 values (1,1,'orange'),(1,2,'large'),(2,1,'yellow'),(2,2,'medium'),(3,1,'green'),(3,2,'small');
44
select max(case col when 1 then val else null end) as color from t1 group by row;
50
# CASE and argument types/collations aggregation into result
52
CREATE TABLE t1 SELECT
53
CASE WHEN 1 THEN _latin1'a' COLLATE latin1_danish_ci ELSE _latin1'a' END AS c1,
54
CASE WHEN 1 THEN _latin1'a' ELSE _latin1'a' COLLATE latin1_danish_ci END AS c2,
55
CASE WHEN 1 THEN 'a' ELSE 1 END AS c3,
56
CASE WHEN 1 THEN 1 ELSE 'a' END AS c4,
57
CASE WHEN 1 THEN 'a' ELSE 1.0 END AS c5,
58
CASE WHEN 1 THEN 1.0 ELSE 'a' END AS c6,
59
CASE WHEN 1 THEN 1 ELSE 1.0 END AS c7,
60
CASE WHEN 1 THEN 1.0 ELSE 1 END AS c8,
61
CASE WHEN 1 THEN 1.0 END AS c9,
62
CASE WHEN 1 THEN 0.1e1 else 0.1 END AS c10,
63
CASE WHEN 1 THEN 0.1e1 else 1 END AS c11,
64
CASE WHEN 1 THEN 0.1e1 else '1' END AS c12
72
THEN _latin1'a' COLLATE latin1_danish_ci
73
ELSE _latin1'a' COLLATE latin1_swedish_ci
77
SELECT CASE _latin1'a' COLLATE latin1_general_ci
78
WHEN _latin1'a' COLLATE latin1_danish_ci THEN 1
79
WHEN _latin1'a' COLLATE latin1_swedish_ci THEN 2
83
CASE _latin1'a' COLLATE latin1_general_ci WHEN _latin1'A' THEN '1' ELSE 2 END,
84
CASE _latin1'a' COLLATE latin1_bin WHEN _latin1'A' THEN '1' ELSE 2 END,
85
CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_swedish_ci THEN '1' ELSE 2 END,
86
CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_bin THEN '1' ELSE 2 END
90
# COALESCE is a CASE abbrevation:
92
# COALESCE(v1,v2) == CASE WHEN v1 IS NOT NULL THEN v1 ELSE v2 END
94
# COALESCE(V1, V2, . . . ,Vn ) =
95
# CASE WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,Vn) END
97
# Check COALESCE argument types aggregation
100
CREATE TABLE t1 SELECT COALESCE(_latin1'a',_latin2'a');
102
CREATE TABLE t1 SELECT COALESCE('a' COLLATE latin1_swedish_ci,'b' COLLATE latin1_bin);
103
CREATE TABLE t1 SELECT
104
COALESCE(1), COALESCE(1.0),COALESCE('a'),
105
COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
106
COALESCE('a' COLLATE latin1_bin,'b');
107
explain extended SELECT
108
COALESCE(1), COALESCE(1.0),COALESCE('a'),
109
COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
110
COALESCE('a' COLLATE latin1_bin,'b');
111
SHOW CREATE TABLE t1;
115
SELECT 'case+union+test'
117
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END;
119
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END;
121
SELECT 'case+union+test'
123
SELECT CASE '1' WHEN '2' THEN 'BUG' ELSE 'nobug' END;
126
# Bug #17896: problem with MIN(CASE...)
129
create table t1(a float, b int default 3);
130
insert into t1 (a) values (2), (11), (8);
131
select min(a), min(case when 1=1 then a else NULL end),
132
min(case when 1!=1 then NULL else a end)
133
from t1 where b=3 group by b;
138
# Tests for bug #9939: conversion of the arguments for COALESCE and IFNULL
141
CREATE TABLE t1 (EMPNUM INT);
142
INSERT INTO t1 VALUES (0), (2);
143
CREATE TABLE t2 (EMPNUM DECIMAL (4, 2));
144
INSERT INTO t2 VALUES (0.0), (9.0);
146
SELECT COALESCE(t2.EMPNUM,t1.EMPNUM) AS CEMPNUM,
147
t1.EMPNUM AS EMPMUM1, t2.EMPNUM AS EMPNUM2
148
FROM t1 LEFT JOIN t2 ON t1.EMPNUM=t2.EMPNUM;
150
SELECT IFNULL(t2.EMPNUM,t1.EMPNUM) AS CEMPNUM,
151
t1.EMPNUM AS EMPMUM1, t2.EMPNUM AS EMPNUM2
152
FROM t1 LEFT JOIN t2 ON t1.EMPNUM=t2.EMPNUM;
156
--echo End of 4.1 tests
159
# #30782: Truncated UNSIGNED BIGINT columns
161
create table t1 (a int, b bigint unsigned);
162
create table t2 (c int);
163
insert into t1 (a, b) values (1,4572794622775114594), (2,18196094287899841997),
164
(3,11120436154190595086);
165
insert into t2 (c) values (1), (2), (3);
166
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1
167
join t2 on t1.a=t2.c order by d;
168
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1
169
join t2 on t1.a=t2.c where b=11120436154190595086 order by d;
172
--echo End of 5.0 tests