建库建表
drop database if exists jbxx; create database jbxx; use jbxx; create table grjbxx( ryid int auto_increment not null primary key, sfzh varchar(50) not null, hbh varchar(50) not null, xm varchar(50) not null, xb varchar(4) not null, mz varchar(50) not null, csrq date not null, hj varchar(50) not null, zz varchar(50) not null, lxdh varchar(20) not null, gmt_create datetime not null default current_timestamp ); insert into grjbxx(sfzh,hbh,xm,xb,mz,csrq,hj,zz,lxdh) values(\'37032219000101001x\',\'3703220105\',\'张三\',\'男\',\'汉族人\',\'1900-02-05\',\'山东省高青\',\'高青县*住宅小区*号院*模块*户号\',\'1390000000\'); select * from grjbxx;
查看xm的分类统计情况
SELECT xm,count(*) from grjbxx group by xm;
只表明xm反复行的统计情况
SELECT xm,count(*) from grjbxx group by xm having count(*)>1;
表明xm反复的任何纪录信息内容
SELECT * from grjbxx where xm in(SELECT xm from grjbxx group by xm having count(xm)>1);
表明sfzh反复的任何纪录信息内容
SELECT * from grjbxx where sfzh in(SELECT sfzh from grjbxx group by sfzh having count(sfzh)>1);
删除表中不必要的sfzh重复记录,重复记录是依据单独字段名(sfzh)来分辨,只留出ryid较大的纪录
delete from grjbxx where sfzh in(SELECT sfzh from grjbxx group by sfzh having count(sfzh)>1) and ryid not in(select max(ryid) from grjbxx group by sfzh having count(sfzh)>1);
数据库查询改动
use mytest; --开启mytest数据库查询 SELECT * from stu; --查看STU表所有纪录 ALTER TABLE stu MODIFY gmt_modified datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP; --改动gmt_modified字段名非空,升级时自动升级 ALTER TABLE stu MODIFY gmt_modified datetime NOT NULL; --改动gmt_modified字段名非空 ALTER TABLE stu change gmt_create gmt_create datetime NOT NULL DEFAULT CURRENT_TIMESTAMP; --改动gmt_create是datetime种类,非空,初始值全自动時间 desc stu; --表明stu表结构 UPDATE stu set gmt_modified=\'2018-08-01 01:59:01\'; --升级全部的gmt_modified的值 truncate stu; --清除stu表,仅保存算法设计
合拼几行数据信息到一行,hbh同样的合拼到一行
SELECT hbh,count(xm) as 总数 ,GROUP_CONCAT(xm) as 组员 from grjbxx GROUP BY hbh;
查看前5条纪录
select * from grjbxx LIMIT 5;
统计好几个数据信息,运用虚似表dual表明
SELECT (SELECT count(DISTINCT sfzh) from grjbxx) 总人数, (SELECT count(DISTINCT sfzh) from sb where sblb=\'01\') 个人社保 from DUAL;
查看date字段名日期区域内的纪录,表明 1980-01-01 00:00:00 –1999-12-31 00:00:00,2个界限都包含,这段时间范畴的纪录,
select * from grjbxx WHERE csrq BETWEEN ‘1980-01-01’ and ‘1999-12-31’;
查看一段时间内的纪录
select * from grjbxx WHERE gmt_create >=’2019-01-23 00:00:00′ and gmt_create<= ‘2019-01-23 14:53:06’;
日期变换
SELECT DATE_FORMAT(20140614162458,\'%Y-%m-%d %H:%i:%s\'); SELECT DATE_FORMAT(\'2014-02-24\',\'%Y-%m-%d %H:%i:%s\'); SELECT DATE_FORMAT(\'2014-02-24\',\'%Y-%m-%d\') SELECT str_to_date(\'2014-02-24\',\'%Y-%m-%d\') SELECT DATE_FORMAT(gmt_create,\'%Y-%m-%d\') from grjbxx; ORCAL 日期转换 select to_date(\'2004-05-07 13:23:44\',\'yyyy-mm-dd hh24:mi:ss\') from dual
应用substr函数查看,SUBSTR(str,pos,len)表明:从pos逐渐的部位,提取len字符(空缺也算标识符)。
SELECT * from grjbxx where substr(sfzh,1,6)=’370322′
REPLACE(str,oldstring,newstring)替换函数有三个主要参数,它将string中的oldstring更换为newstring字符串数组。
UPDATE grjbxx SET hbh=REPLACE(hbh,’2′,’a’)
删掉自提高外键约束,从1逐渐
1、撤销自提高
ALTER TABLE grjbxx MODIFY ryid int;
2、撤销外键约束
ALTER TABLE grjbxx DROP PRIMARY key;
3、升级全部id为0
UPDATE grjbxx set ryid=0;
4、设定外键约束自提高
ALTER TABLE grjbxx CHANGE ryid ryid int not NULL PRIMARY KEY auto_increment;
表明百分号%
select concat(round(fs/100*100,2),’%’) from sb;
测算>=60纪录数占有率
select round(sum(case when fs>=60 then 1 else 0 end)/count(fs),2) from sb;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。