mysql基础语句总结

显示数据库
show databases;
创建一个名为mydb1的数据库:
create database mydb1( if not exists);
创建一个使用UTF8字符集的mydb2的数据库(注意不是UTF-8):
create database if not exists mydb2 character set UTF8;
删除数据库mydb1
drop databese (if exists)mydb1;
显示刚才创建的数据库mydb1的定义信息
show create database mybd2;
使用数据库mydb2
use mydb2;
显示数据库中的表
show tables;
创建一张表student_info ,其中包含id,name,age,sex,score五个字段:
create table table student_info(id int,name varchar20),age int,sex varchar8),score int);
查看表student_info 的结构:
desc student_info;
向表student_info 中插入数据:
若不写字段内容,则只能按照字段一一对应插入:
insert into student_info values(1,'王二',22,'男',80);
也可以只给对应字段插入数据,其他缺省:
insert into student_info (name,age,sex) values('李依',23,'女');
修改表:
修改表中score列名为math
alter table student_info  change column score  math int;
删除表中math列
alter table student_info drop column math;
向表中增加列math_score、english_score、chinese_score;
alter table student_info add math_score/english_score/chinese_score int;
把表中name列的长度修改为50
alter table student_info modify name varchar50);
修改表的字符集
alter table student_info character set UTF8/GBK/GB2312;
将所有学生的register_time都设为2017:
update student_info set register_time=2017;
加条件修改,where关键字(where 后若有多项则应用括号把每一项括起来,一项则可省略括号):
将学生李四的英语成绩修改为70:
update student_info set english_score=70 where name ='李四';
将张三的数学成绩加10分:
update student_info set math_score+=10 where name ='张三';
我们可以看出alterupdate都是修改表信息,但是alter是对表结构的修改,而update是对表中内容的修改。
查询表:   
查询所有学生信息
select * from student_info;(如果知道所有列名,也可将* 替换为所有列名,中间用逗号隔开,但比较麻烦,不推荐)
查询所有学生的语文成绩
select name,chinese_score from student_info;
过滤(distinct) 表中重复的年龄,即每个年龄的只保留一条:
select distinct age from student_info;
使用别名(别名要加上双引号):
select name "姓名",math_score+english_score+chinese_score "总分" from student_info;
范围(条件)查询,where关键字(加上not则表示不在该范围的查询):
查询表中name为张三的人的所有信息:
select * from student_info where name='张三';
查询表中name不为NULL的同学信息:
select * from student_info where name is not null;
查询数学成绩在80-90之间的同学信息:
①select * from student_info where (math_score >=80) and (math_score <= 90);select * from student_info where math_score (not) between 80 and 90;
查询英语成绩为85,90,和95的同学:
①select name,english_score from student_info where(english_score =85) or(english_score =90) or(english_score =95);select name,english_score from student_info where english_score (not) in(85,90,95);       
模糊查找,like关键字:
查找所有姓’李‘的同学(%哪则模糊匹配哪部分):
select name from student_info where name like '李%';
查找所以名字中包含’李‘字的同学;
select name from student_info where name like '%李%';
查询表中所有记录也可写成:
select * from student_info where name like '%';(%也可以写多个,但没必要)
查询所有姓’李‘且名字为两个字的同学信息(后面有几个字符则几个下划线):
select * from student_info where name like '李_';
排序使用关键字 order by (排序字段用数值型 ,默认为升序):
降序 desc  
select * from student_info order by math_score desc;
升序 asc
select * from student_info order by math_score asc;
总结:order by后面可以跟如下内容:
①字段
select * from student_info order by age desc;
②表达式
select * from student_info order by (math_score+english_score+chinese_score) desc;
③别名(别名不用加双引号)
select name "姓名",math_score+english_score+chinese_score "总分" from student_info order by 总分 desc;
④列号(按在select中的列的排序来显示,如下例中按总分降序排列)
select name, math_score+english_score+chinese_score from student_info order by 2 desc;
删除表中信息或表:
删除表中id为3的同学的所有信息:
delete from student_info where id=3;
删除表:
delete from student_info;//一行一行删,速度慢,删除后表结构还在,只是内容被删除。
drop tableif exists)student_info;//整个表删,速度快表不在了。
truncate table  student_info;//删除表中内容后再重建表,删除后表结构还在,只不过内容没有了与delete功能一样。
一些统计函数:
count() 统计总数:
统计表中的总人数:
①select count(*) "总人数" from student_info;select count(id) "总人数" from student_info;
统计表中数学成绩大于80的人数:
select count(*) from student_info where (math_score >80);
sum() 求和:
求张三的总成绩:
select sum(math_score+english_score+chinese) from student_info where (name ='张三');
avg() 求平均值:
求全班学生的数学平均分:
select avg(math_score) from student_info;
求全班同学的总平均分:
select avg(math_score+english_score+chinese ) from student_info;
select avg(math_score+english_score+chinese ) from student_info where (name='张三');注意 这种写法是不对的,不能求出张三的平均分,因为math_score+english_score+chineses是一整项,而不是三个单独的成绩。
max() ,min() 求最值:
筛选班级里数学成绩最高和最低的同学:
select name,max(math_score),min(math_score) from student_info;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值