数据库
首页 > 数据库> > sql的学习

sql的学习

作者:互联网

1. having与sum的区别?

 

2. group by all?

 

3. group by

group by的话 会把第一条遇到的数据,作为展示进行筛选出来。

group by entity_id;把相同entity_id重复的合并

常用的意义:

 

4.聚合函数first?

 

5. count(1)的意义

 

6. 测试例子,自定义group by 的规则

CREATE TABLE `user_table` (
  `id` int(11) NOT NULL,
  `user_name` varchar(32) DEFAULT NULL,
  `pass_word` varchar(64) DEFAULT NULL,
  `email` varchar(200) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `activated` tinyint(4) DEFAULT NULL,
  `num` varchar(151) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='            ';

-- ----------------------------
-- Records of user_table
-- ----------------------------
INSERT INTO `user_table` VALUES ('1', 'dream', '123', '1@qq.com', null, '1', '10');
INSERT INTO `user_table` VALUES ('2', 'dream1', '1234', '123@qq.com', '2019-08-05 15:53:15', '0', '11');
INSERT INTO `user_table` VALUES ('3', 'peter', 'abc', 'peter1@qq.com', '2019-08-05 15:53:15', '0', '50');
INSERT INTO `user_table` VALUES ('4', 'peter1', 'abcd', 'peter2@qq.com', '2019-08-05 15:53:15', '0', '100');

 

SELECT
	user_name,
	email,
	count(1) AS 数量
FROM
	`user_table`
GROUP BY
	user_name
HAVING
	数量 = 2;

SELECT
	CASE
WHEN `user_name` IN ('dream', 'dream1') THEN
	'all_dream'
ELSE
	`user_name`
END AS `names`,
 SUM(num)
FROM
	user_table
GROUP BY
	`names`;

SELECT
	CASE
WHEN `user_name` IN ('dream', 'dream1') THEN
	'all_dream'
ELSE
	`user_name`
END AS `names`,
 SUM(num)
FROM
	user_table
GROUP BY
	CASE
WHEN `user_name` IN ('dream', 'dream1') THEN
	'all_dream'
ELSE
	`user_name`
END;

比如按照自定义规则进行分组,把两个名字或者多个属性合成不同的属性,最后在展示!!

 

 

TODO:动态数据源切换项目。

 

标签:name,DEFAULT,学习,user,sql,table,NULL,dream
来源: https://blog.csdn.net/pmdream/article/details/98494183