数据库mysql原生代码基本操做

建立表:

CREATE TABLE `biao` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '测试表', `createtime` datetime DEFAULT NULL COMMENT '时间', `title` varchar(10) CHARACTER SET latin1 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

删除表:
DROP TABLE IF EXISTS `biao`;
DROP TABLE IF EXISTS t1,t2;

显示表结构,简写desc:describe biao

显示表结构语句:show create table biao
修改表名:alter table old rename new
显示表结构:show tables
修改字段类型:alter table biao modify id int(5)
修改表字段,新旧字段同样和modify就同样功能:
ALTER TABLE test ADD qqq VARCHAR(10) DEFAULT '1' NOT NULL
ALTER TABLE test CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123';
删除字段:ALTER TABLE ims_mm DROP ttt
定义主键 primary key
非空 not null
惟一性 unique
默认值 default
自动 auto_increment
函数:
DROP TABLE IF EXISTS `biao`;
DROP TABLE IF EXISTS t1,t2;

显示表结构,简写desc:describe biao

显示表结构语句:show create table biao
修改表名:alter table old rename new
显示表结构:show tables
修改字段类型:alter table biao modify id int(5)
修改表字段,新旧字段同样和modify就同样功能:
ALTER TABLE test ADD qqq VARCHAR(10) DEFAULT '1' NOT NULL
ALTER TABLE test CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123';
删除字段:ALTER TABLE ims_mm DROP ttt
定义主键 primary key
非空 not null
惟一性 unique
默认值 default
自动 auto_increment
函数:
describe biaoshow create table biao
修改表名:alter table old rename new
显示表结构:show tables
修改字段类型:alter table biao modify id int(5)
修改表字段,新旧字段同样和modify就同样功能:
ALTER TABLE test ADD qqq VARCHAR(10) DEFAULT '1' NOT NULL
ALTER TABLE test CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123';
删除字段:ALTER TABLE ims_mm DROP ttt
定义主键 primary key
非空 not null
惟一性 unique
默认值 default
自动 auto_increment
函数:
alter table old rename new
显示表结构:show tables
修改字段类型:alter table biao modify id int(5)
修改表字段,新旧字段同样和modify就同样功能:
ALTER TABLE test ADD qqq VARCHAR(10) DEFAULT '1' NOT NULL
ALTER TABLE test CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123';
删除字段:ALTER TABLE ims_mm DROP ttt
定义主键 primary key
非空 not null
惟一性 unique
默认值 default
自动 auto_increment
函数:
show tables
修改字段类型:alter table biao modify id int(5)
修改表字段,新旧字段同样和modify就同样功能:
ALTER TABLE test ADD qqq VARCHAR(10) DEFAULT '1' NOT NULL
ALTER TABLE test CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123';
删除字段:ALTER TABLE ims_mm DROP ttt
定义主键 primary key
非空 not null
惟一性 unique
默认值 default
自动 auto_increment
函数:
alter table biao modify id int(5)ALTER TABLE test ADD qqq VARCHAR(10) DEFAULT '1' NOT NULL
ALTER TABLE test CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123';
删除字段:ALTER TABLE ims_mm DROP ttt
定义主键 primary key
非空 not null
惟一性 unique
默认值 default
自动 auto_increment
函数:
ALTER TABLE ims_mm DROP ttt

随机数 rand()
链接字符串 concat('中国','打日本')
转换小写 lcase('ABC') lower
转换大写 upper ucase
去除空格 trim(str)
获取日期函数

curdate()+0 返回20160916 curdate() 2016-09-16 

时间测试

curtime() 

日期时间 now()
时间戳编码

UNIX_TIMESTAMP(), UNIX_TIMESTAMP(now()) 日期转换时间戳 FROM_UNIXTIME( 1539659520) 时间戳转换成日期时间 

月份spa

month('2016-04-28') 

季度,1,2,3,4code

QUARTER('2016-04-28') 

星期orm

DAYOFWEEK('2016-04-28') 1是星期日 weekday 0是星期1 

天数排序

DAYOFYEAR('2016-04-28') 1-366 

年 year
分钟 minute
小时 hour
秒 second
版本version索引

聚合函数
avg(col) 平均值
count(*) 记录数
min(col) max(col)最小 最大值
sum(col)求和token

SET NAMES utf8 设置编码
基本查询rem

select * from biao 

条件

select * from biao where id=1 

结果字段不重复

select distinct id from biao

排序,默认升序asc,降序desc

select * from biao order by id; select * from biao order by id desc ,time asc; 

分组,having 限定条件

select * from biao group by sex; select * from biao group by sex having; 

查询条数

select * from biao limit 4 返回4条 select * from biao 4,3 返回3条,从第5条记录开始 

内链接两个功能相同

select a,b,c from A inner join B on A.id = B.id; select a,b,c from A,B where A.id=B.id; 

左右链接

select * from A left join B on A.id=B.id; select * from A right join B on A.id=B.id; 

子查询

select id from Table where id2 in(select id3 from Table2) 

合并结果集

select * from A union all select * from B select * from A union select * from B 去重复 

表或字段别名




select id as ID from A as a
 

插入

 
insert into Table(id,name) values (null,'Li') ;插入数据 insert into T(id,name) values (null,'Li'),(null,"Zhao"); insert into T values (null,"Li",null,"数据");插入全部列 insert into T(id,name) select id,name from T2; 查询插入 insert ignore into Table(id,name) values (null,'Li') ; 惟一索引插入 有效防止重复数据 
 

修改

 
update T set name='Li' where id=1; 更新 update t set num=num+1 where id=1 自动加一 update T left join T1 on T.id=T1.id set u='1',u1='2' where id=1;多表更新 update T set user=replace(user,'a','a1') 替换表字段 
 

删除

 
delete from T where id =1;删除 delete from T where id in(1,2,3);删除多条 
 

清空

 
truncate table test

经常使用的查询:
select * from ims_rank where DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= date(createtime); 七天以前数据 30天 select * from ims_rank where to_days(createtime) =to_days(now()); 今天数据 select * from ims_rank where TO_DAYS( NOW( ) ) -TO_DAYS( createtime) <= 1 昨天今天数据 select * from ims_rank where DATE_FORMAT( createtime,'%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )本月 select * from ims_rank where PERIOD_DIFF( date_format(now( ) , '%Y%m' ) , date_format( createtime, '%Y%m' ) ) =1 上月 select * from ims_rank where YEARWEEK(date_format(createtime,'%Y-%m-%d')) =YEARWEEK(now()); 本周(第一天星期日) select * from ims_rank where YEARWEEK(date_format(createtime,'%Y-%m-%d')) = YEARWEEK(now())-1; 上周数据
相关文章
相关标签/搜索