insert 常用的语句
insert 语法
insert into <表名> ( < 字段名 1>,..,< 字段名 n> ) values ( 值 1 ), ......,( 值 n );
-- 注意:
1, 后面的值 1 对应的是字段名 1 的值, 依此类推.
2, 没有指定字段时, 表示对应每个字段都会插入数据
3, 如果值是字符串的话要用 ' 单引号引起了
仅在 name 下插入一个名字
mysql> insert into xixi(name) values('xiaoer');
插入两行数据
mysql> insert into xixi values(2,'zhangsan'),(3,'lisi');
-- 表示在 xixi 表中对应的 2 字段插入 zhangsan 和 3 字段中插入了 lisi
在表中的多个字段插入多条数据
mysql> insert into xixi(name,age) values('zhangsan',28),('lisi',29);
-- 在 xixi 表中插入 name 值为 zhangsan,age 字段值为 28 和 name 值为 lisi,age 字段值为 29
查看表内容
mysql> select * from xixi;
使用 insert 复制表结构及内容, 产生附表.
mysql> create xixi2 test2 like xixi; -- 创建新表 xixi2 复制 xixi 表的结构
mysql> insert into xixi2 select * from xixi; -- 把 xixi 表的内容插入新的 xixi2 表中
update 常用的语句
update 更改数据库
为安全一定要加上 where 条件更新表内容
将字段中的 lisi 改为 haha
mysql> update xixi set name='haha' where name='lisi';
将 xixi 表中字段 id=5 的行把 name 值改为 maliu
update xixi set name=maliu where id=5;
一次修改多个值
mysql>update xxi set name-maliu,age=27 where id=5
更改后查看
select * from xixi;
delete 常用的语句
- delete from xixi; #逻辑删除, 一行一行删.
- truncate table xixi; #物理删除, 效率高.
根据指定的条件删除语句
mysql> delete from xixi where name='zhangsan';
如: 从表中找出 age 大于 50 的进行降序排列, 删除第一个.
delete from xixi where age>50 order by age desc limit 1;
删除之后查看下
mysql> select * from test2;
在 mysql 命令加上选项 - U 后使用 update 与 delete 命令的时候不加 where 条件不会执行.
select 常用语句
命令语法
select 字段 1, 字段 2,... from 表名 [ where 表达式 and 表达式 ]
-- 其中, select,from,where 是不能随便改的, 是关键字, 支持大小写.
查看表中的所有信息
select * from xixi;
查看表中的 id 和 name
select id,name from xixi;
查看 id 等于 2 的信息
select id,name from xixi where id=2;
查看名字是 zhangsan 的记录
select id,name from xixi where name='zhangsan';
查看 id 大于 2 的记录
select id,name from xixi where id>2;
查看 id 大于 2 并且 小于 4 的记录
select id,name from xixi where id>2 and id<4;
查看表中所有 id 大于等于 5 并且小于等于 10 的数据
select * from xixi where id>= 5 and <= 10;
查看表中所有 id 等于 5 或者等于 10 的数据
select * from xixi where id = 5 or id = 1
查看 id 不等于 5 和 10 之间的数据
- select * from xixi where id not between 5 and 10
- select * from xixi where id <5 or id> 10;
like 结合通配符使用查询 % 表示任意长度的任意字符, _ 表示任意单个字符 , 还支持正则表达式
select * from xixi where name like 'a%'; -- 查看 name 字段中所有以 a 开头的数据
使用 in 关键字指定列表如:
找出 id 等于 5,6,7 中的任意一行的所有数据
select * from xixi where id in (5,6,7);
select * from xixi where id not in (5,6,7); --not in 也就是取反的意思
limit 指定行
select id,name from xixi limit 2; -- 显示前两行
select id,name from xixi limit 2,3; -- 显示第 2 行后的三行
asc 正向排序查看 (不加默认就是正向排序)
select id,name from xixi order by id asc; -- 查看 id 和 name 字段以 id 字段进行正向排序
desc 反向排序查看
select id,name from xixi order by id desc; -- 查看 id 和 name 字段并以 id 字段进行反向排序
select * from xixi order by id desc; -- 查看所有并以 id 字段进行反向排序显示
distinct 去重
select distinct age from xixi; --xixi 表的 age 字段中去除重复的行显示
select 分组查询
一些聚合函数的常用示例
group by 分组
count() 统计指定列的数量
avg() 求指定列平均值
sum() 求指定列的和
min() 显示指定列的最小值
max() 显示指定列的最大值
count() 显示指定列中非 null 值的个数
group_concat() 分组显示指定列的值
查询 students 表中的以性别为分组, 求出分组后的年龄之和
select gender,sum(age)from student group gender --gender 性别 --age 年龄
查询 student 表中以 classid 分组, 显示平均值大于 22 的 classid
select?classid,avg(age)?as?avgage?from?student?group?by?classid?having?avgage?>?22;
查询 student 表中以性别字段 gender 分组, 显示各组中的年龄大于 18 的学员的年龄总和
select?sum(age)?from?students?where?age?>?19?group?by?gender;
来源: http://www.bubuko.com/infodetail-2610218.html