mysql
查看数据库
show databases;
进入数据库
use test;
查看表数据
show tables
查看表结构
desc user;
退出
建库
create database db1
charset utf8;
创建表
create table stu (
id int,
name varchar(20),
gender char(1),
birthday date
);
插入数据
insert into stu
values(6,'张三','男','1996-8-4');
更新、修改数据
update stu
set gender='女',birthday='1998-8-4'
where id=7;
删除数据
delete from stu
where id>8;
查询数据
select id,fname,sal,dept_id
from emps
where id=122;
关键字
去重
select distinct a,b from ...
去除a,b字段组合的重复值
排序(desc降序 asc升序{默认是升序})
select id,fname,sal,dept_id
from emps
where dept_id=50
order by sal desc;
防止sql注入
{语句这么写就可以完成sql注入,因为语句是完全成立的}
select * from user
where username='def'
and password='1' or '1'='1'
函数
字符串函数
char_length(字符串) 字符数
length(字符串) 字节数
left(字符串, length) 获得左侧字符
substring(字符串, start, length) 截取字符串
instr(字符串, 子串) 查找子串位置
concat(s1,s2,s3...) 字符串连接
lpad(字符串,8,'*') 左侧填充
数字函数
ceil(数字) 向上取整到个位
floor(数字) 向下取整到个位
round(数字, 2) 四舍五入到小数点2位
truncate(数字, 2) 舍弃到小数点2位
rand() 随机数[0, 1)
日期函数
now() 当前日期时间
curdate() 当前日期
curtime() 当前时间l extract(字段 from 日期) 抽取指定字段的值
date_add(日期, interval 字段 值) 在指定字段上加一个值
datediff(日期1, 日期2) 两个日期之间相差的天数
多行函数
sum() 和
avg() 平均
max() 最大
min() 最小
count() 行数
多行函数不能和其他普通字段一起查询
多个多行函数可以一起查询
多行函数会忽略null值
count(*) 记行数
count(distinct a) 去除重复再计数
例字
最低工资值
select id,fname,min(sal)
from emps;