MySQL数据库

来源:互联网 发布:js下载图片保存到本地 编辑:程序博客网 时间:2024/06/08 14:33
创建表格stud
<span style="font-size:14px;"> create table stud( sno varchar(15) not null primary key, sname varchar(15) not null, age int, saddress varchar(15) );</span>




表格添加数据:
<span style="font-size:14px;">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,'武侠');</span>


查看stud表的数据:

<span style="font-size:14px;">select * from stud;</span>



给列名取别名显示:

<span style="font-size:14px;">select sno as 编号,sname as 姓名,age as 年龄, saddress as 地址 from stud;</span>




select 复杂查询:

查询stud表格中age大于等于24的:

<span style="font-size:14px;">select * from stud where age>=24;</span>




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

<span style="font-size:14px;">select * from stud where age>=20 and age <=30;</span>


还有一种方法:

<span style="font-size:14px;">select * from stud where age between 20 and 30;</span>




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

select * from stud where age=20 or 30;


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

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

<span style="font-size:14px;">select * from stud where age in(20,22,30);</span>


有一个和in相对的:not in

<span style="font-size:14px;">select * from stud where age not in(20,22,30);</span>



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

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

查询名字是张开头的:

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




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

<span style="font-size:14px;">select * from stud where sname like '张_';</span>



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

<span style="font-size:14px;">select * from stud where sname like '张__';</span>




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

<span style="font-size:14px;">select * fom stud where sname like '%三%';</span>




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

<span style="font-size:14px;">select * from stud where sname like '%三%' and age >30;</span>




为表格增加一列:

<span style="font-size:14px;">alter table stud add column sex char(1);</span>

省略column 也可以添加

<span style="font-size:14px;">alter table stud add sex char(1);</span>



从stud表格删除sex列

<span style="font-size:14px;">alter table stud drop sex;</span>

也可以用:

<span style="font-size:14px;">alter table stud drop column sex;</span>

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

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



<span style="font-size:14px;">update stud set age=20 where age=null;</span>
这一句是不起作用的,因为这个无法用来判断age是否为null。

应该用下面这句:


<span style="font-size:14px;">elect stud set age=20 where age is null;</span>
作用是:如果stud表格中哪行的age为null,就设置age为20.



如果是判断哪个为空字符,就直接可以用=''  来判断。

例:

<span style="font-size:14px;">select * from stud where saddress='';</span>

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


将saddress为纽约的改为硅谷

<span style="font-size:14px;">update stud set saddress='硅谷' where saddress='纽约'; </span>

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



同时修改多个字段的值:

<span style="font-size:14px;">update stud set sname='ROSE', saddress='北京' where sno='1002';</span>



删除名字是悟空的行:

<span style="font-size:14px;">delete from stud where sname='悟空';</span>




知识点:

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:

<span style="font-size:14px;">select sname,sno,age from aview where age<40;</span>



聚合函数:


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

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

<span style="font-size:14px;">select count(*) from stud;select count(1) from stud;</span>



统计age不为空的行数:

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

<span style="font-size:14px;">select count(age) from stud;</span>



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

<span style="font-size:14px;">select avg(age) as averageAge from stud;</span>



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

<span style="font-size:14px;">select round(avg(age)) as averageAge2 from stud;</span>

还有:

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

<span style="font-size:14px;">select  sum(age)  as sunAge from stud;select max(age) as maxAge from stud;select min(age) as minAge from stud;</span>



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

<span style="font-size:14px;">select sname , age from stud where age = ( selectt min(age) from stud );</span>
这样用in也可以:

<span style="font-size:14px;">select sname ,age from stud where age in(select min(age) from stud);</span>


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

<span style="font-size:14px;">insert into stud value('1009','李白',10,'湖南');</span>
再查年龄最小的那个人的年龄:

<span style="font-size:14px;">select age from stud where age=(select min(age) from stud);</span>


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

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


<span style="font-size:14px;">select distinct age from stud where age = (select min(age) from stud);</span>




排序-升序和降序:

按年龄升序排:

<span style="font-size:14px;">select * from stud order by age asc;</span>


按年龄降序排:

<span style="font-size:14px;">select sno,sname,age from stud order by age desc;</span>



exists存在判断

<span style="font-size:14px;">select sname,age from stud where exists (select * from stud where age = 20);</span>

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

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

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




分组  group by 

<span style="font-size:14px;">select saddress , avg(age) as 平均年龄 from stud group by saddress;</span>

按照saddress来分组,求出每组的平均年龄。

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




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

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



有2个固定搭配:

排序:

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

分组:

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


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



字符串处理函数

<pre name="code" class="sql"><span style="font-size:14px;">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</span>

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

演示left();

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

<span style="font-size:14px;">select * from stud where left(saddress,2)='湖南';</span>



串联字符串:

<span style="font-size:14px;">select concat(snon,sname,saddress) as 串联字符串 from stud;</span>



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

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

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




删除名字是悟空的行:

<span style="font-size:14px;">delete from stud where sname='悟空';</span>




知识点:

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:

<span style="font-size:14px;">select sname,sno,age from aview where age<40;</span>



聚合函数:


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

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

<span style="font-size:14px;">select count(*) from stud;select count(1) from stud;</span>



统计age不为空的行数:

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

<span style="font-size:14px;">select count(age) from stud;</span>



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

<span style="font-size:14px;">select avg(age) as averageAge from stud;</span>



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

<span style="font-size:14px;">select round(avg(age)) as averageAge2 from stud;</span>

还有:

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

<span style="font-size:14px;">select  sum(age)  as sunAge from stud;select max(age) as maxAge from stud;select min(age) as minAge from stud;</span>



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

<span style="font-size:14px;">select sname , age from stud where age = ( selectt min(age) from stud );</span>
这样用in也可以:

<span style="font-size:14px;">select sname ,age from stud where age in(select min(age) from stud);</span>


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

<span style="font-size:14px;">insert into stud value('1009','李白',10,'湖南');</span>
再查年龄最小的那个人的年龄:

<span style="font-size:14px;">select age from stud where age=(select min(age) from stud);</span>


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

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


<span style="font-size:14px;">select distinct age from stud where age = (select min(age) from stud);</span>




排序-升序和降序:

按年龄升序排:

<span style="font-size:14px;">select * from stud order by age asc;</span>


按年龄降序排:

<span style="font-size:14px;">select sno,sname,age from stud order by age desc;</span>



exists存在判断

<span style="font-size:14px;">select sname,age from stud where exists (select * from stud where age = 20);</span>

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

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

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




分组  group by 

<span style="font-size:14px;">select saddress , avg(age) as 平均年龄 from stud group by saddress;</span>

按照saddress来分组,求出每组的平均年龄。

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




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

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



有2个固定搭配:

排序:

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

分组:

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


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



字符串处理函数

<pre name="code" class="sql"><span style="font-size:14px;">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</span>

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

演示left();

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

<span style="font-size:14px;">select * from stud where left(saddress,2)='湖南';</span>



串联字符串:

<span style="font-size:14px;">select concat(snon,sname,saddress) as 串联字符串 from stud;</span>



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

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

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

0 0
原创粉丝点击