My SQL语句实现增删改查
2017-09-26 本文已影响0人
_凉笙
修改密码:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
查询数据库:Show databases;
Paste_Image.png
创建数据库(helloworld):create database helloworld;
Paste_Image.png
使用数据库(helloworld):use helloworld;
Paste_Image.png
创建表格:create table mytable();
create table mytable(//创建表名为mytable
id int auto_increment,//auto_increment为自动增长
username varchar(30),
password varchar(30),
primary key(id)//设置主键为id
);
Paste_Image.png
查看表结构:desc mytable;
Paste_Image.png
删除表:drop table mytable;
Paste_Image.png
插入数据:insert into mytable(要插入到哪列) values(插入的值)
insert into mytable(username,password) values('hero','1234');
Paste_Image.png
简单查询列表:select * from mytable;
Paste_Image.png
更新数据:
//更新id=1的username为China
update mytable set username='China' where id=1;
Paste_Image.png
修改用户名和密码用逗号隔开就OK
Paste_Image.png
删除指定Id的数据
delete from mytable where id=1;
Paste_Image.png
查询前4条数据
select * from mytable limit 4;
Paste_Image.png
查询指定的某几条数据
//从第一条后面开始查找,查询后面的三条
select * from mytable limit 1,3;
Paste_Image.png
查询制定id的用户名
select username from mytable where id=5;
Paste_Image.png
根据id排序
select * from mytable order by id desc;//order表示排序 desc表示反向
Paste_Image.png
转换列表顺序,并修改列表名。
select password Password,username Name from mytable;
Paste_Image.png
四舍五入:round()
select round(4.44,1);//四舍五入,保留后小数一位
Paste_Image.png
直接舍去,直接入
select floor(4.6);//直接舍去
select ceiling(4.2);//直接入
Paste_Image.png
去重(distinct)
select distinct username as Name from mytable;
//去除username里面重复的数据并且更改列名
Paste_Image.png
字符串操作
concat
left
length
reverse
replace
date_format %m %b %d %y %Y %T %f
(详情:http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format)
get_format(datetime,'EUR' 'ISO' 'USA')
dayofweek
quarter
week
monthname
distinct(去重)
where条件
1,数字 > < = >= <= <>
2,字符串 = '' > < = >= <= <> !=
逻辑操作
is 仅用is null或is not null
and or not
and 优先级> or
范围判断
in (not in)
between (not between) 示例:select * from category where category_id between 1 and 9;
like (not like) % _
示例1:select * from category where name like 'A%';
关于NULL的条件
is NULL
is not NULL