2001-09-28 07:05:54 +02:00
drop table if exists t1;
select CASE "b" when "a" then 1 when "b" then 2 END;
2000-12-28 02:56:38 +01:00
CASE "b" when "a" then 1 when "b" then 2 END
2
2001-09-28 07:05:54 +02:00
select CASE "c" when "a" then 1 when "b" then 2 END;
2000-12-28 02:56:38 +01:00
CASE "c" when "a" then 1 when "b" then 2 END
NULL
2001-09-28 07:05:54 +02:00
select CASE "c" when "a" then 1 when "b" then 2 ELSE 3 END;
2000-12-28 02:56:38 +01:00
CASE "c" when "a" then 1 when "b" then 2 ELSE 3 END
3
2001-09-28 07:05:54 +02:00
select CASE BINARY "b" when "a" then 1 when "B" then 2 WHEN "b" then "ok" END;
2000-12-28 02:56:38 +01:00
CASE BINARY "b" when "a" then 1 when "B" then 2 WHEN "b" then "ok" END
2003-07-18 09:42:35 +02:00
ok
2001-09-28 07:05:54 +02:00
select CASE "b" when "a" then 1 when binary "B" then 2 WHEN "b" then "ok" END;
2000-12-28 02:56:38 +01:00
CASE "b" when "a" then 1 when binary "B" then 2 WHEN "b" then "ok" END
2003-07-18 09:42:35 +02:00
ok
2001-09-28 07:05:54 +02:00
select CASE concat("a","b") when concat("ab","") then "a" when "b" then "b" end;
2000-12-28 02:56:38 +01:00
CASE concat("a","b") when concat("ab","") then "a" when "b" then "b" end
a
2001-09-28 07:05:54 +02:00
select CASE when 1=0 then "true" else "false" END;
2000-12-28 02:56:38 +01:00
CASE when 1=0 then "true" else "false" END
false
2001-09-28 07:05:54 +02:00
select CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END;
2000-12-28 02:56:38 +01:00
CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END
one
2003-10-30 11:57:26 +01:00
explain extended select CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings:
2004-05-13 22:47:20 +02:00
Note 1003 select (case 1 when 1 then _latin1'one' when 2 then _latin1'two' else _latin1'more' end) AS `CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END`
2001-09-28 07:05:54 +02:00
select CASE 2.0 when 1 then "one" WHEN 2.0 then "two" ELSE "more" END;
2000-12-28 02:56:38 +01:00
CASE 2.0 when 1 then "one" WHEN 2.0 then "two" ELSE "more" END
two
2001-09-28 07:05:54 +02:00
select (CASE "two" when "one" then "1" WHEN "two" then "2" END) | 0;
2000-12-28 02:56:38 +01:00
(CASE "two" when "one" then "1" WHEN "two" then "2" END) | 0
2
2001-09-28 07:05:54 +02:00
select (CASE "two" when "one" then 1.00 WHEN "two" then 2.00 END) +0.0;
2000-12-28 02:56:38 +01:00
(CASE "two" when "one" then 1.00 WHEN "two" then 2.00 END) +0.0
2.00
2001-09-28 07:05:54 +02:00
select case 1/0 when "a" then "true" else "false" END;
2000-12-28 02:56:38 +01:00
case 1/0 when "a" then "true" else "false" END
false
2001-09-28 07:05:54 +02:00
select case 1/0 when "a" then "true" END;
2000-12-28 02:56:38 +01:00
case 1/0 when "a" then "true" END
NULL
2001-09-28 07:05:54 +02:00
select (case 1/0 when "a" then "true" END) | 0;
2000-12-28 02:56:38 +01:00
(case 1/0 when "a" then "true" END) | 0
NULL
2001-09-28 07:05:54 +02:00
select (case 1/0 when "a" then "true" END) + 0.0;
2000-12-28 02:56:38 +01:00
(case 1/0 when "a" then "true" END) + 0.0
NULL
2001-09-28 07:05:54 +02:00
select case when 1>0 then "TRUE" else "FALSE" END;
2000-12-28 02:56:38 +01:00
case when 1>0 then "TRUE" else "FALSE" END
TRUE
2001-09-28 07:05:54 +02:00
select case when 1<0 then "TRUE" else "FALSE" END;
2000-12-28 02:56:38 +01:00
case when 1<0 then "TRUE" else "FALSE" END
FALSE
2001-09-28 07:05:54 +02:00
create table t1 (a int);
insert into t1 values(1),(2),(3),(4);
select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
2001-01-31 03:47:25 +01:00
fcase count(*)
0 2
2 1
3 1
2003-10-30 11:57:26 +01:00
explain extended select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
Warnings:
2004-07-20 07:48:28 +02:00
Note 1003 select (case `test`.`t1`.`a` when 1 then 2 when 2 then 3 else 0 end) AS `fcase`,count(0) AS `count(*)` from `test`.`t1` group by (case `test`.`t1`.`a` when 1 then 2 when 2 then 3 else 0 end)
2001-09-28 07:05:54 +02:00
select case a when 1 then "one" when 2 then "two" else "nothing" end as fcase, count(*) from t1 group by fcase;
2001-01-31 03:47:25 +01:00
fcase count(*)
nothing 2
one 1
two 1
2001-09-28 07:05:54 +02:00
drop table t1;
2002-12-26 14:24:40 +01:00
create table t1 (row int not null, col int not null, val varchar(255) not null);
insert into t1 values (1,1,'orange'),(1,2,'large'),(2,1,'yellow'),(2,2,'medium'),(3,1,'green'),(3,2,'small');
select max(case col when 1 then val else null end) as color from t1 group by row;
2002-12-16 21:16:57 +01:00
color
orange
yellow
green
2002-12-26 14:55:22 +01:00
drop table t1;
2003-07-14 16:28:36 +02:00
SET NAMES latin1;
2003-07-18 09:42:35 +02:00
CREATE TABLE t1 SELECT
CASE WHEN 1 THEN _latin1'a' COLLATE latin1_danish_ci ELSE _latin1'a' END AS c1,
CASE WHEN 1 THEN _latin1'a' ELSE _latin1'a' COLLATE latin1_danish_ci END AS c2,
CASE WHEN 1 THEN 'a' ELSE 1 END AS c3,
CASE WHEN 1 THEN 1 ELSE 'a' END AS c4,
CASE WHEN 1 THEN 'a' ELSE 1.0 END AS c5,
CASE WHEN 1 THEN 1.0 ELSE 'a' END AS c6,
CASE WHEN 1 THEN 1 ELSE 1.0 END AS c7,
2003-11-20 21:06:25 +01:00
CASE WHEN 1 THEN 1.0 ELSE 1 END AS c8,
CASE WHEN 1 THEN 1.0 END AS c9
2003-07-18 09:42:35 +02:00
;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
2004-12-07 14:47:00 +01:00
`c1` varchar(1) character set latin1 collate latin1_danish_ci NOT NULL default '',
`c2` varchar(1) character set latin1 collate latin1_danish_ci NOT NULL default '',
`c3` varchar(1) NOT NULL default '',
`c4` varchar(1) NOT NULL default '',
`c5` varchar(3) NOT NULL default '',
`c6` varchar(3) NOT NULL default '',
2003-11-20 21:06:25 +01:00
`c7` double(3,1) NOT NULL default '0.0',
`c8` double(3,1) NOT NULL default '0.0',
`c9` double(3,1) default NULL
2003-12-10 05:31:42 +01:00
) ENGINE=MyISAM DEFAULT CHARSET=latin1
2003-07-18 09:42:35 +02:00
DROP TABLE t1;
SELECT CASE
WHEN 1
THEN _latin1'a' COLLATE latin1_danish_ci
ELSE _latin1'a' COLLATE latin1_swedish_ci
END;
ERROR HY000: Illegal mix of collations (latin1_danish_ci,EXPLICIT) and (latin1_swedish_ci,EXPLICIT) for operation 'case'
SELECT CASE _latin1'a' COLLATE latin1_general_ci
WHEN _latin1'a' COLLATE latin1_danish_ci THEN 1
WHEN _latin1'a' COLLATE latin1_swedish_ci THEN 2
END;
ERROR HY000: Illegal mix of collations (latin1_general_ci,EXPLICIT), (latin1_danish_ci,EXPLICIT), (latin1_swedish_ci,EXPLICIT) for operation 'case'
SELECT
CASE _latin1'a' COLLATE latin1_general_ci WHEN _latin1'A' THEN '1' ELSE 2 END,
CASE _latin1'a' COLLATE latin1_bin WHEN _latin1'A' THEN '1' ELSE 2 END,
CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_swedish_ci THEN '1' ELSE 2 END,
CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_bin THEN '1' ELSE 2 END
;
CASE _latin1'a' COLLATE latin1_general_ci WHEN _latin1'A' THEN '1' ELSE 2 END CASE _latin1'a' COLLATE latin1_bin WHEN _latin1'A' THEN '1' ELSE 2 END CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_swedish_ci THEN '1' ELSE 2 END CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_bin THEN '1' ELSE 2 END
1 2 1 2
2003-07-14 16:28:36 +02:00
CREATE TABLE t1 SELECT COALESCE(_latin1'a',_latin2'a');
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (latin2_general_ci,COERCIBLE) for operation 'coalesce'
CREATE TABLE t1 SELECT COALESCE('a' COLLATE latin1_swedish_ci,'b' COLLATE latin1_bin);
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,EXPLICIT) and (latin1_bin,EXPLICIT) for operation 'coalesce'
CREATE TABLE t1 SELECT
COALESCE(1), COALESCE(1.0),COALESCE('a'),
COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
COALESCE('a' COLLATE latin1_bin,'b');
2003-10-30 11:57:26 +01:00
explain extended SELECT
COALESCE(1), COALESCE(1.0),COALESCE('a'),
COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
COALESCE('a' COLLATE latin1_bin,'b');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL No tables used
Warnings:
2004-08-31 09:06:38 +02:00
Note 1003 select coalesce(1) AS `COALESCE(1)`,coalesce(1.0) AS `COALESCE(1.0)`,coalesce(_latin1'a') AS `COALESCE('a')`,coalesce(1,1.0) AS `COALESCE(1,1.0)`,coalesce(1,_latin1'1') AS `COALESCE(1,'1')`,coalesce(1.1,_latin1'1') AS `COALESCE(1.1,'1')`,coalesce((_latin1'a' collate latin1_bin),_latin1'b') AS `COALESCE('a' COLLATE latin1_bin,'b')`
2003-07-14 16:28:36 +02:00
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`COALESCE(1)` int(1) NOT NULL default '0',
`COALESCE(1.0)` double(3,1) NOT NULL default '0.0',
2004-12-07 14:47:00 +01:00
`COALESCE('a')` varchar(1) NOT NULL default '',
2003-07-14 16:28:36 +02:00
`COALESCE(1,1.0)` double(3,1) NOT NULL default '0.0',
2004-12-07 14:47:00 +01:00
`COALESCE(1,'1')` varchar(1) NOT NULL default '',
`COALESCE(1.1,'1')` varchar(3) NOT NULL default '',
`COALESCE('a' COLLATE latin1_bin,'b')` varchar(1) character set latin1 collate latin1_bin NOT NULL default ''
2003-12-10 05:31:42 +01:00
) ENGINE=MyISAM DEFAULT CHARSET=latin1
2003-07-14 16:28:36 +02:00
DROP TABLE t1;