mysql 查竖表 mysql横竖表转换
转载
- 一、Mysql竖表和横表转化:
- 1. 竖表转横表
- my01表:


- select
- t.year,
- sum(case when t.month=1 then t.amount end) m1,
- sum(case when t.month=2 then t.amount end) m2,
- sum(case when t.month=3 then t.amount end) m3,
- sum(case when t.month=4 then t.amount end) m4
- from my01 t group by year;
-


- 在这里不要误会sum函数,在不求和的情况下可以不要的。
- select t.year,
- (case when t.month=1 then t.amount end) m1,
- (case when t.month=2 then t.amount end) m2,
- (case when t.month=3 then t.amount end) m3,
- (case when t.month=4 then t.amount end) m4
- from my01 t group by year;
- 主要的是case when 函数:
- mysql数据库中CASE WHEN语句。
- case when语句,用于计算条件列表并返回多个可能结果表达式之一。
- CASE 具有两种格式:
- 简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。
- CASE 搜索函数计算一组布尔表达式以确定结果。
- 两种格式都支持可选的 ELSE 参数。
- 语法
- 1.简单 CASE 函数:
- CASE input_expression
- WHEN when_expression THEN result_expression
- [ ...n ]
- [
- ELSE else_result_expression
- END
- 2.CASE 搜索函数:
- CASE
- WHEN Boolean_expression THEN result_expression
- [ ...n ]
- [
- ELSE else_result_expression
- END
- 参数
- input_expression
- 是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft? SQL Server? 表达式。
- WHEN when_expression
- 使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。
- 占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。
- THEN result_expression
- 当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。
- result expression 是任意有效的 SQL Server 表达式。
- ELSE else_result_expression
- 当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。
- WHEN Boolean_expression
- 使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。
- 结果类型
- 从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。有关更多信息,请参见数据类型的优先顺序。
- 结果值
- 简单 CASE 函数:
- 计算 input_expression,然后按指定顺序对每个 WHEN 子句的 input_expression = when_expression 进行计算。
- 返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。
- 如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
- CASE 搜索函数:
- 按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。
- 返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。
- 如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
- A. 使用带有简单 CASE 函数的 SELECT 语句
- 在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。
- SELECT Category =
- CASE type
- WHEN 'popular_comp' THEN 'Popular Computing'
- WHEN 'mod_cook' THEN 'Modern Cooking'
- WHEN 'business' THEN 'Business'
- WHEN 'psychology' THEN 'Psychology'
- WHEN 'trad_cook' THEN 'Traditional Cooking'
- ELSE 'Not yet categorized'
- END,
- CAST(title AS varchar(25)) AS 'Shortened Title',
- price AS Price
- FROM titles
- WHERE price IS NOT NULL
- ORDER BY type, price
- COMPUTE AVG(price) BY type
- B. 使用带有简单 CASE 函数和 CASE 搜索函数
- SELECT 'Price Category' = CASE
- WHEN price IS NULL THEN 'Not yet priced'
- WHEN price < 10 THEN 'Very Reasonable Title'
- WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
- ELSE 'Expensive book!'
- END,
- CAST(title AS varchar(20)) AS 'Shortened Title'
- FROM titles
- ORDER BY price
- C、GROUP BY 子句中的 CASE:
- SELECT 'Number of Titles', Count(*)
- FROM titles
- GROUP BY
- CASE
- WHEN price IS NULL THEN 'Unpriced'
- WHEN price < 10 THEN 'Bargain'
- WHEN price BETWEEN 10 and 20 THEN 'Average'
- ELSE 'Gift to impress relatives'
- END
- 2. 横表转竖表
- my011表:


- select year,'1' as month,m1 as amount from my011 union
- select year,'2' as month,m2 as amount from my011 union
- select year,'3' as month,m3 as amount from my011 union
- select year,'4' as month,m4 as amount from my011
- order by year,month,amount;


- Union操作符:
- UNION 用于合并两个或多个 SELECT 语句的结果集,并消去表中任何重复行。
- UNION 内部的 SELECT 语句必须拥有相同数量的列,列也必须拥有相似的数据类型。
- 同时,每条 SELECT 语句中的列的顺序必须相同.
- 如果不想去掉重复的行,可以使用union all。
- 如果子句中有order by,limit,需用括号()包起来。推荐放到所有子句之后,即对最终合并的结果来排序或筛选。
- (select * from a order by id) union (select * from b order id);
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。