数据库
首页 > 数据库> > MySQL 编写出某列数据所占总和百分比 SQL实例

MySQL 编写出某列数据所占总和百分比 SQL实例

作者:互联网

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for person_base_info
-- ----------------------------
DROP TABLE IF EXISTS `person_base_info`;
CREATE TABLE `person_base_info`  (
  `uuid` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `email` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`uuid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of person_base_info
-- ----------------------------
INSERT INTO `person_base_info` VALUES ('w1e2233rf', '5', '深圳', '222@qq.om');
INSERT INTO `person_base_info` VALUES ('we33212', '2', '深圳', '225452@qq.om');
INSERT INTO `person_base_info` VALUES ('we33rf', '1', '深圳', '224232@qq.om');
INSERT INTO `person_base_info` VALUES ('we5663rf', '4', '北京', '28822@qq.om');
INSERT INTO `person_base_info` VALUES ('wewq33rf', '3', '深圳', '222@qq.om');

SET FOREIGN_KEY_CHECKS = 1;

查询出所有数据:

SELECT  *   FROM person_base_info

 

 查询地址所占比:

SELECT
a.address,
a.cont AS countAddress,
b.sum AS sumAddress,
CONCAT( ROUND( a.cont / b.sum * 100, 2 ), '', '%' ) AS percent 
FROM
( SELECT address, COUNT( address ) AS cont FROM person_base_info GROUP BY address ) a,(
SELECT
COUNT(*) AS sum 
FROM
person_base_info 
) b

 

标签:info,SET,某列,编写出,utf8,person,base,SQL,NULL
来源: https://www.cnblogs.com/weigy/p/13381351.html