mysql基础

从最开始的创建数据库,创建表,创建列开始写起,再到常用的EXISTS函数,SELECT 复杂查询,模糊查询LIKE,创建视图 等深入学习。
为了对单词加深印象,全部在DOS下演示!
 

创建数据库、表

create database hncu character set utf8;

创建名为hncu编码为utf-8的数据库。

use hncu;

 打开hncu这个数据库。(必须要打开一个数据库才能在这个数据库下面创建table哦)

创建表格stud 

create table stud(
 sno varchar(15) not null primary key,
 sname varchar(15) not null,
 age int,
 saddress varchar(15)
 );

表格添加数据:

insert into stud values('1001','Jack',20,'纽约');
insert into stud values('1002','Tom',30,'纽约');
insert into stud values('1003','张三',24,'湖南益阳');
insert into stud values('1004','张四',15,'湖南长沙');
insert into stud values('1005','李四',22,'湖南益阳');
insert into stud values('1006','张三丰',80,'武侠');
insert into stud values('1007','郭襄',75,'武侠');
insert into stud values('1008','灭绝师太',10,'武侠');

查看stud表的数据:

select * from stud;

 给列名取别名显示:

select sno as 编号,sname as 姓名,age as 年龄, saddress as 地址 from stud;

select 复杂查询:

查询stud表格中age大于等于24的所有信息:

select * from stud where age>=24;

查询stud表格中age大于等于20且小于等于30的数据:

select * from stud where age>=20 and age <=30;

还有一种方法between ..and...(两者之间包括边界值):

select * from stud where age between 20 and 30;

查询年龄等于20或者年龄等于30的stud表中的数据:

select * from stud where age=20 or 30;

还有一种方法:用 in();

查询年龄为20,22和30的stud表中的数据:

select * from stud where age in(20,22,30);

 

有一个和in相对的:not in

select * from stud where age not in(20,22,30);

模糊查询LIKE: '%'匹配所有, '_'匹配单字符  ---必须和LIKE共同使用:

也就是说通配符只能在有like的情况下使用,如果是和= 一起使用,那就只是普通的字符了。

查询名字是张开头的:

select * from stud where sname like '张%';

 

查询名字张开头的,而且名字只有2个字符的:

select * from stud where sname like '张_';

查询名字张开头的,而且名字只有3个字符的:

select * from stud where sname like '张__';

查询名字中带有‘三’的:

select * fom stud where sname like '%三%';

查询名字中带有‘三’的而且年龄大于30的:

select * from stud where sname like '%三%' and age >30;

 

为表格增加一列:

alter table stud add column sex char(1);

省略column 也可以添加

alter table stud add sex char(1);

 

从stud表格删除sex列

alter table stud drop sex;

也可以用:

alter table stud drop column sex;

判断NULL值时,不能用‘=’号判断,而是用is:

先插入一行数据,让他的age为null;

insert into stud values('1009','悟空',null,null);

select * from stud;

update stud set age=20 where age=null;

 

这一句是不起作用的,因为这个无法用来判断age是否为null。

应该用下面这句:

update stud set age=20 where age is null;

作用:如果stud表格中哪行的age为null,就设置age为20.

 

如果是判断哪个为空字符,就直接可以用='' (单引号空字符串)来判断

例:

select * from stud where saddress='';

 

作用:如果stud表中有saddress为空(注意!是空,不是null),就查询显示出来。

 

将saddress为纽约的改为硅谷

update stud set saddress='硅谷' where saddress='纽约';

注意:不是这里不能写成 update table stud set...;

同时修改多个字段的值:

update stud set sname='ROSE', saddress='北京' where sno='1002';

删除名字是悟空的行:

delete from stud where sname='悟空';

知识点:

select 字段 from 表名 where 条件 and 条件 or 条件

update tableName set 需要设置的值 where 条件

delete from tableName where 条件

创建视图:cerate view 视图名 as select 子句

(虚表)---只存在内存中

create view aview as select * from stud where age>20;

从视图aview中查询年龄小于40的sname,age,ano:

select sname,sno,age from aview where age<40;

 

聚合函数:

 

统计非null数据的行数:(*号和1 代表只要表中一行有非null的列数据,这一行就是非null)

一般要专门给个别用: as 别名

select count(*) from stud;
select count(1) from stud;

统计age不为空的行数:

也就是age为null就不会被统计进去。

select count(age) from stud;

显示出stud表中所有age的平均值:

select avg(age) as averageAge from stud;

 

显示所有age平均值的四舍五入:

select round(avg(age)) as averageAge2 from stud;

还有:

Sum求和,
Max求最大值,
Min求最小值。

select  sum(age)  as sunAge from stud;
select max(age) as maxAge from stud;
select min(age) as minAge from stud;

 

选择年龄最小的那个人的名字和年龄:

select sname , age from stud where age = ( select min(age) from stud );

这样用in也可以:

select sname ,age from stud where age in(select min(age) from stud);

 

再创建一个年龄等于10的行:

insert into stud value('1009','李白',10,'湖南');

查询年龄最小的那个人的年龄:

select age from stud where age=(select min(age) from stud);

我们可以看到,因为有2个数据的年龄都是最小值,所有显示了2行,但是它们是重复的,完全没必要显示2行。

这个时候我们就要用到:distinct ,把完全相同的行,合并显示!

select distinct age from stud where age = (select min(age) from stud);

 

排序-升序和降序:

按年龄升序排:

select * from stud order by age asc;

按年龄降序排:

select sno,sname,age from stud order by age desc;

 

exists存在判断:

select sname,age from stud where exists (select * from stud where age = 20);

exists (select * from stud where age=20) ---只要存在age=20的,就返回true、

也就是exists(...) 是判断括号内的表达式是不是null的,如果是null则返回false,否则返回true;

此句因为stud存在age=20的行,所以会输出所有的sname,age。
 

分组  group by :

查询各个地方的平均年龄:

select saddress,avg(age) as 平均年龄 from stud group by saddress;

只要saddress不同就在不同的组!

按照saddress分组后每组的年龄总和:

select saddress,sum(age) as 年龄总和 from stud group by saddress;

 

有2个固定搭配:

排序:

select ... from ... where ... order by ...  

分组:

select ... from ... group by ... having ... (条件判断在having后面,不是用where)

这里的sum(age)也可以用as 别名  取一个别名,在判断的时候直接可以用别名。

 

字符串处理函数

Length(str) - 求字符串长度
Ltrim(str) - 去掉左边的空格
Rtrim(str) - 去掉右边的空格
trim(str)  - 去掉两边的空格
Left(str,n); - 从左边取出n个字符
Right(str,n); - 从右边取出n个字符
Substring(str,begin,end) -返回子串
Reverse(str) –返回颠倒的字符串
Lower(str) - 转成小写
Upper(str) - 转成大写
Concat(Str,str…..)串联字符串。
Instr(str,s) – 返回s在str中出面的位置,没有则返回0

这里就只选取几个来演示了:

演示left();

显示saddress开始2个字符为湖南的行

select * from stud where left(saddress,2)='湖南';

 

串联字符串:

select concat(sno,sname,saddress) as 串联字符串 from stud;

 

instr(str,s) 返回s在str中出面的位置,没有则返回0

其实就是返回子字符串第一次出现的位置(从1开始计数)

select sname,instr(sname,'三') as ind from stud;

原创

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值