准备工作:建表插入数据
建一个account表
CREATE TABLE `account` (
`id` int(11) NOT NULL,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`money` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
插入几条数据
INSERT INTO `account` VALUES (1, 'lucy', '800');
INSERT INTO `account` VALUES (2, 'tom', '1300');
INSERT INTO `account` VALUES (3, 'lisi', '800');
INSERT INTO `account` VALUES (4, 'zhangshan', '1300');
INSERT INTO `account` VALUES (5, 'mi', '100');
如下结果图
问题: 找出money字段重复的钱数 和 重复的次数
sql语句
SELECT money, COUNT(*) AS sum
FROM account
GROUP BY money
HAVING COUNT(*) > 1
总结
group>group by 分组,要和having一块用,作为分组的条件。
order by ,默认按照升序排序,
order by 后面跟上排序字段,排序字段可以放多个,多个采用逗号间隔
如果存在 where 子句,那么 order by 必须放到where 语句后面