例子:表名:project
字段名:name,age,sex,class
(1) 更新字段内容:字段name为a的更新为b(name不为a的不更新)
update project set name = 'b' where name = 'a'
(2) 删除字段: 删除字段name
ATLTER Table ‘project’
DROP COLUMN ‘name'
(3) 表重命名 :project重命名为pro
第一种: rename table project to pro
第二种: ALTER Table project rename to pro
(4) 更新字段为拼接字段(更新字段name为字段age和字段sex拼接起来的内容,中间加空格)
update project set name = concat(age," ",sex)
(5) 统计一班中男女分别有多少人
第一种: select sex,count(sex) c from project where class = 1 group by sex
此种不统计sex为null的数量,sex为null一行c显示为0
第二种:select sex,count(1) c from project where class = 1 group by sex
此种统计sex为null的数量,sex为null一行c显示为实际数值
第三种:不想显示sex为null的情况,以下两种都可以
select sex,count(1) c from project where class = 1 and sex is not null group by sex
select sex,count(sex) c from project where class = 1 and sex is not null group by sex
(6)统计一班中最大年龄是多大
JAVA语句:@Select("select max(age) from project where class = #{class}")
Integer getMaxAge(Integer class);
说明,此语句返回类型一定要是为Integer,不能为int。int 不能为null,无法处理sql语句返回为null的情况,会一直报错;Integer型可以为null,sql返回为null时可以根据需要进行处理。
(7)外表关联统计:统计六年级中所有班级的男女生数量(外表名为school,年级 grade)
select sex,count(1) c from project where class in (select class_id from school where grade = 6) group by sex(group by sex:按性别分组,按sex统计数量,则必须按sex分组)
(8)外表关联:六年级中所有班级的所有人的信息
select * from project where class in (select class_id from school where grade = 6)
说明:三层嵌套同理两层嵌套 如: select * from project where class in (select class_id from school where grade in (select school_id from nation where id = #{id}))
(9) 清空表
delete from project
(10)删除六年级
delete from project where class = 6
(11) 索引六年级之外的所有学生(sql中不等于号用 <> 表示)
select * from project where class <> 6
(12)索引一班年龄最大的学生的信息(多条)
select * from project where class =1 and age = (select max(age) from project where class = 1)
附加题:索引一条一班年龄最大的学生的信息,如下: select * from project where class =1 and age = (select max(age) from project where class = 1) limit 1
(13)删除school表中不在本项目的班级
delete from school where class_id not in (select class from project)
(14)索引按姓名排列的学生信息(查重名)
先索引一班全部学生名称:select name from project where class = 1
按名称索引学生信息: select * from project where name in (select name from project where class = 1)
索引增加班级条件(否则其他班学生与本班有重名也会索引到): select * from project where name in (select name from project where class = 1) and class = 1
(15)索引一班学生并按年龄排序
select * from project where class =1 order by age
(16)索引名称为liu的学生的年龄
select age from project where name = 'liu' limit 1
说明:limit 1: 取索引到的第一条数据,返回值为int或Integer;
如果不加 limit 1,则返回List
(17)模糊匹配name
select * from project where name like '%name%'
(18)索引name为liu的学生的信息(按类型索引)
select * from project where name = 'liu'
(19)统计一班中年龄为16的学生数量
select count(1) c from project where class = 1 and age = 16
(20)索引一班学生年龄,去重显示
select DISTINCT age from project where class =1
(21)复制表结构和数据:创建表b,复制表a的结构和数据
create table b like a;
insert into b select * from a;
(22)一下语句可直接在navicat中执行
SELECT VERSION( )服务器版本信息
SELECT DATABASE( )当前数据库名 (或者返回空)
SELECT USER( )当前用户名
SHOW STATUS服务器状态
SHOW VARIABLES服务器配置变量
(22)查询重复数据(比如表a和表b之间建立了中间表a_b,字段包括a_id,b_id,查询a,b有没有重复关联)
select count(a_id) as repeations,a_id,b_id from ruleset_rule group by a_id,b_id having repeations > 1;
说明:repeations可以是任意字段,用于统计重复数,repeations > 1表示显示重复数大于1的数据
(23)比较运算符:= 和 <=> 等于和安全等于的区别
等于
2=3 -> 0 ; null=null -> NULL
2<=>3 -> 0 ; null<=>null -> 1 (安全等于 比较 null ,返回结果是 true)
(24)取消主键a
ALTER TABLE `a`
DROP PRIMARY KEY;
(23)求和:计算一班所有学生学习时间的总和
select sum(learn_time) from project where class = 1;
(24)以逆字母顺序显示公司名称,并以数字顺序显示顺序号:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
说明:order by 后面有多个参数时,每个参数应分别指定顺序还是倒序(默认为顺序 asc),排序顺序按参数从左到右先后排序
(25) 全部更新: project 表中 school_id 都设为1
update project set school_id = 1;