数据库操作
1.基础与创建
#显示数据库
show databases;
#选择数据库
use databasename;
#显示数据表
show tables;
#显示表结构
describe tablename;
#创建数据库并设置编码utf-8 多语言
create database `examples` default character set utf8 collate utf8_general_ci;
#创建表
create table test(
id int(10) unsigned zerofill not null auto_increment,
email varchar(40) not null,
ip varchar(15) not null,
state int(10) not null default '-1',
primary key (id)
)engine=InnoDB;
#创建索引
alter table test add index (cn,id);
#删除索引
alter table test drop index cn
2.增
#插入数据
insert into test (id,email,ip,state) values(2,'qq@qq.com','127.0.0.1','0');
3.删
#删除数据库
drop database examples;
#删除表
drop table test;
#删除数据
delete from test where id = 1;
4.改
#修改数据
update test set id='1',email='q@qq.com' where id=1;
#重命名表
alter table test_old rename test_new;
#添加列
alter table test add cn int(4) not null;
#修改列
alter table test change id id1 varchar(10) not null;
#删除列
alter table test drop cn;
5.查
#查数据
#取所有数据
select * from test;
#取前两条数据
select * from test limit 0,2;
#查含有qq字符 _表示一个 %表示多个
select * from test email like '%qq%'
#降序desc
select * from test order by id asc;
#id不含2,3或者去掉not表示含有
select * from test id not in('2','3');
#数据在1,10之间
select * from test timer between 1 and 10;
#-----------表连接知识------------#
#等值连接又叫内链接 inner join 只返回两个表中连接字段相等的行
#写法1
select * from A inner join B on A.id = B.id;
#写法2
select * from A,B where A.id = B.id;
#写法3 表的临时名称
select a.id,a.title from A a inner join B b on a.id=b.id and a.id=1;
#添加as字句
select a.id as ID,a.title as 标题 from A inner join B on A.id=B.id;
#左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录
select * from A left join B on A.id = B.id;
#复杂连接
select * from A left join (B,C,D) on (B.i1=A.i1 and C.i2=A.i2 and D.i3 = A.i3);
#右连接又叫外连接 right join 返回右表中所有记录和左表中连接字段相等的记录
select * from A right join B on A.id = B.id;
#完整外部链接 full join 返回左右表中所有数据
select * from A full join B on A.id = B.id;
#交叉连接 没有where字句 返回卡迪尔积
select * from A cross join B;
#--------------表连接结束----------------#
6.索引与常用函数
#-----------------常用函数------------------#
#绝对值
abs(-1);
#pi值
pi();
#平方根
sqrt(2);
#取余-2
mod(-5,3);
#进位+1 结果11 ceil(10.0)结果10
ceil(10.6);
#取整 10
floor(10.6);
#四舍五入到整数 结果3
round(2.5);
#保留两位小数 结果2.50
round(2.5,2);
#取小数后3位不四舍五入 2.523
truncate(2.5234,3);
#符号函数 返回-1 0还是0 正数返回1
sign(-2);
#2的3次幂 或e的2次幂
pow(2,3),exp(2);
#求对数
log(2),log10(2);
#角度弧度转换
radians(180),degrees(0.618);
#正弦和反正弦 类似cos acos tan atan
sin(0.5),asin(0.5)
#计算字符长度
length('hi')
#合并字符串
concat('1',1,'hi')
#从开头第1个字符开始到0个结束,替换成后边字符串,0表示在最前边插入
insert('12345',1,0,'7890');
#转成大写和小写
ucase('a'),lcase('A')
#返回前两个字符和后两个字符
left('abcd',2),right('abcd',2);
#删除空格
ltrim(' 0 '),rtrim(' 0 '),trim(' 0 ')
#替换输出12090
replace('1234567890','345678','0');
#取字符 输出12 1是位置 2是长度
substring('12345',1,2)
#取得234位置是2
instr('1234','234');
#反序输出4321
reverse('1234');
#返回日期
current()
#返回时间
curtime()
#返回日期时间
now()
#当前月份 monthname 英文月份
month(now())
#星期英文 dayofweek()1是星期天 weekday()1是星期二
dayname(now())
#本年第多少周
week(now())
#今天是本年第多少天 今天是本月第多少天
dayofyear(now()),dayofmonth(now())
#返回年月日 时分秒
year(now()),month(now()),day(now()),hour(now()),minute(now()),second(now())
#转换时间为秒和还原
time_to_sec(now()),sec_to_time(3600*8);
#mysql版本
version()
#当前连接的数据库 没有为null
database()
#获取用户名
user()
#加密字符串
md5('a')
#ascii值97
ascii('a')
#返回二进制 十六进制 八进制
bin(100),hex(100),oct(100)
#各种进制相互转换
conv(10001,2,8);
#生成0到1之间随机数
rand()
#暂停秒数
sleep(0.02)