欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL判断查询,拼接查询实战

程序员文章站 2024-03-16 22:25:58
...

测试一

事前准备:实例用的表结构和数据


/*
Navicat MySQL Data Transfer
Source Server         : work
Source Server Version : 50616
Source Host           : localhost:3306
Source Database       : souvc
Target Server Type    : MYSQL
Target Server Version : 50616
File Encoding         : 65001
Date: 2018-03-08 18:19:18
*/
 
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `state` tinyint(4) DEFAULT NULL,-- 1:有效;2:无效
  `type` tinyint(4) DEFAULT NULL,-- 1:普通员工;2:中层员工;3:高层员工
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '张三', '1', '1');
INSERT INTO `user` VALUES ('2', '李四', '1', '2');
INSERT INTO `user` VALUES ('3', '王五', '2', '3');
INSERT INTO `user` VALUES ('4', '赵六', '2', '2');

一、case when 函数

语法1:

CASE fieldname
	WHEN value1 THEN result1
	WHEN value2 THEN result2
	WHEN value3 THEN result3
	……
	ELSE defaultresult
END

实例1:

SELECT
	`id` '用户ID',
	`name` '用户名称',
	(
		CASE `state`
			WHEN 1 THEN '正常'
			WHEN 2 THEN '不正常'
			ELSE NULL
		END
	) '用户状态'
FROM
	`user`

结果1:

MySQL判断查询,拼接查询实战

语法2:

CASE
	WHEN condition1 THEN result1
	WHEN condition2 THEN result2
	WHEN condition3 THEN result3
	……
	ELSE defaultresult
END

实例2:

SELECT
	`id` '用户ID',
	`name` '用户名称',
	(
		CASE 
			WHEN `state`=1 THEN '正常'
			WHEN `state`=2 THEN '不正常'
			ELSE NULL
		END
	) '用户状态'
FROM
	`user`

结果2

MySQL判断查询,拼接查询实战

二、if函数

case when 函数处理复杂逻辑判断比较常用,如果只是处理一次判断,更简单的方式是IF函数

语法:

IF(condition,result1,result2)

实例:

SELECT
	`id` '用户ID',
	`name` '用户名称',
	IF(state=1,'正常','不正常') '用户状态'
FROM
	`user`

结果:

MySQL判断查询,拼接查询实战

三、elt函数

语法:

ELT(condition,result1,result2,result3,...)

注意:condition的值必须是顺序1、2、3、...才会对应结果result1、result2、result3、...

实例:

SELECT
	`id` '用户ID',
	`name` '用户名称',
	`type`,
	ELT(type,'普通员工','中层员工','高层员工') '用户类型'
FROM
	`user`;

结果:

MySQL判断查询,拼接查询实战

实战

问题描述,计算19年前三个月与18年前三个月销售数据的同比增长率。

并将增长率正负用字段type区分开来。

注意:由于数据量不大,为方便所以使用select * from查询,一般数据量大最好是一个一个把字段输入

SELECT
	*,
IF (增长率 > 0, '1', '2') AS type
FROM
	(
		SELECT
			*, (
				(`b`.`money2` - `a`.`money1`) / `a`.`money1`
			) AS `增长率`
		FROM
			(
				SELECT
					`省份` AS `省份1`,
					sum(`结算金额`) AS money1
				FROM
					`销售表2018`
				WHERE
					date_format(单据日期, '%Y-%m') = "2018-01"
				OR date_format(单据日期, '%Y-%m') = "2018-02"
				OR date_format(单据日期, '%Y-%m') = "2018-03"
				GROUP BY
					`省份1`
			) AS a
		LEFT JOIN (
			SELECT
				`省份` AS `省份2`,
				sum(`结算金额`) AS money2
			FROM
				`销售表2019`
			WHERE
				date_format(单据日期, '%Y-%m') = "2019-01"
			OR date_format(单据日期, '%Y-%m') = "2019-02"
			OR date_format(单据日期, '%Y-%m') = "2019-03"
			GROUP BY
				`省份2`
		) AS b ON a.`省份1` = b.`省份2`
	) AS c

部分原始数据1:

MySQL判断查询,拼接查询实战

结果1:

MySQL判断查询,拼接查询实战

 

最后与经纬度表格连接起来读取,并将两个字段内容做拼接。

部分原始数据2:

SELECT *,concat('19年1-3月总结算金额:',总结算金额,'\n。同比18年1-3月增长率为:',增长率) as info FROM
(SELECT * from 2019销售表含地图) as e
LEFT JOIN
(SELECT
	*,
IF (增长率 > 0, '1', '2') AS type
FROM
	(
		SELECT
			*, (
				(`b`.`money2` - `a`.`money1`) / `a`.`money1`
			) AS `增长率`
		FROM
			(
				SELECT
					`省份` AS `省份1`,
					sum(`结算金额`) AS money1
				FROM
					`销售表2018`
				WHERE
					date_format(单据日期, '%Y-%m') = "2018-01"
				OR date_format(单据日期, '%Y-%m') = "2018-02"
				OR date_format(单据日期, '%Y-%m') = "2018-03"
				GROUP BY
					`省份1`
			) AS a
		LEFT JOIN (
			SELECT
				`省份` AS `省份2`,
				sum(`结算金额`) AS money2
			FROM
				`销售表2019`
			WHERE
				date_format(单据日期, '%Y-%m') = "2019-01"
			OR date_format(单据日期, '%Y-%m') = "2019-02"
			OR date_format(单据日期, '%Y-%m') = "2019-03"
			GROUP BY
				`省份2`
		) AS b ON a.`省份1` = b.`省份2`
	) AS c) as d
ON e.省份=d.省份1

MySQL判断查询,拼接查询实战

结果2:

MySQL判断查询,拼接查询实战

待续