当前位置:Gxlcms > 数据库问题 > MySQL常用技巧

MySQL常用技巧

时间:2021-07-01 10:21:17 帮助过:14人阅读

MySQL对sum()字段进行条件筛选:having

显示按日期汇总且内数量大于10的记录。
SELECT date, count(*) as num FROM tbName GROUP BY date HAVING num>10;
在这里,我们不能用where来筛选超过10的记录,因为表中不存在这样一条记录。相反,having子句可以让我们筛选成组后的各组数据

常用SQL语句

# 创建表
CREATE TABLE tb_test (
id bigint(21) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘自增主键‘,
name varchar(100) NOT NULL DEFAULT ‘‘ COMMENT ‘名称‘,
id_card varchar(40) NOT NULL DEFAULT ‘‘ COMMENT ‘身份证号‘,
user_id int(11) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘用户id‘,
create_time int(11) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘创建时间‘,
is_del tinyint(1) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘是否禁用‘,
PRIMARY KEY (id),
KEY idx_user (name,user_id),
UNIQUE KEY `idx_id_card` (`id_card`),
KEY `idx_created` (`create_time`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT=‘用户信息‘;

预估好数据将来的量,尽量往大理想,如使用 bigint 代替 int

MySQL常用技巧

标签:var   pre   use   efault   btree   汇总   tree   sql   技巧   

人气教程排行