mysql查询基础

增,删,改,查

废话不多说 直接从mysql的增删改查入手.

  • 增加,插入数据(insert):

单条数据语法:insert into tablename (field1,field2,field3) values(value1,value2,value3)

多条数据语法:
insert into tablename(field1,field2) values(v1,v2), (v1,v2)

  • 删除:

delete from tablename [where 条件]

  • 更改(更新):

update tablename set field1 = value1,field2 =value2

查询

1.基础查询:

select 字段 from 表名;

语法和关键字:

1.语法

select 查询:填写要查询的内容查询内容

from 获取: 从哪个数据表中获取数据

where 筛选: 根据你输入的表达式对数据表中的数据进行筛选

group by 分组: 根据你选择的字段对整个表进行分组

having 分组过滤: 对已经分组后数据表进行条件过滤

limit 限制: 限制显示几条数据.

2.关键字:

  • in:查询某个指定集合内的记录

select * from table where condition in (content)

  • and:用来同时添加多个限定条件

select * from table where condition1 and condition2 同时满足条件1和条件2的数据才会出现在查询结果中

  • or:只要满足任意一个条件即可完成查询

select * from table where condition1 and condition2 满足条件1或者条件2都会出现在查询结果中.

  • between and :查询某个在给定范围内的记录

select age from student where age between 10 and 20 查询年龄在10-20之间的学生.

  • distinct: 去重复

select distinct 字段名 from 表名 会将distinct后所跟随的字段名进行去重的过滤操作.

tips: 如果使用distinct指定多个字段, 那么只有指定的多个字段同时完全相同 才会进行过滤操作.

  • like与%与_: 当在查询一个特征的字段的时候,需要使用like进行模糊查询 通过%来进行通配符匹配.

%表示匹配任意字符的字符串,like表示模糊查询,_表示对单个字符进行匹配.

select name from student where student.name like '张%';

这句话表示查询所有以张开头的学生的名字.

3.函数

  • count()函数:统计记录条数

select count(记录) from student

查询student中总共有多少条记录

  • sum()函数:计算表中某个字段值的总和:

select sum(salary) from money

查询工资一共支出了多少.

  • avg()函数:计算某个字段的平均值:

select avg(age) from student

计算师表中的平均年龄.

  • max()函数:返回表中某个字段中的最大值

  • min()函数:返回某个字段的最小值.

4.排序:

  • order By:对整个表的数据进行排序

asc表示升序 desc表示降序

select * from student order by student.score asc;

按学生成绩升序 对表进行排名

2.高级查询

子查询

1、一个询语句中还包含其他查询。其中包含其他查询的查询叫父查询,被包含的查询叫子查询。
2、子查询也可以和UPDATE、INSERT、DELETE一起使用,语法类似于SELECT语句 。
3、子查询中可以再包含子查询,即允许多层嵌套。

子查询单返回值:
select * from student where age> (select age from student where student.name ='张三');

括号中的语句就是子查询,将子查询和比较运算符联合使用时要注意 返回的数据不能超过一行. 否则会报错

子查询多返回值:

select * from student where stuent.id in (select student.id from grade where courseid=1);

当子查询返回有多个值得时候可以通过关键字in来使用.

exists子句:

exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录,反之如果exists里的条 件语句不能返回记录行,则当前loop到的这条记录被丢弃,exists的条件就像一个bool条件,当能返回结果集则为true,不能返回结果集则为 false

什么意思呢?

select * from student where exists (select sno from student where  SNO=0); 

这条语句中会对exists中的语句返回结果进行检查,经过检查没有sno等于0的项.返回为false 查询结果为全空

使用not exists与exists相反, 同样一条语句 会将不符合条件的通通选出来.
多表连接查询
  • 内连接(inner join):

语法: select * from t1 inner join t2 (会得到笛卡尔积的表)

什么是笛卡尔积?
select * from student INNER JOIN sc;
这里两个查询出来的结果条数为 student表的条数*sc表的条数. 这就是笛卡尔积,字段数也会增加.



等值连接与非等值连接:
例: select * from student inner join score on (score.sno = student.sno and score>60  ) 

两个表取得笛卡尔积后,当两个表的的sno相等时且score字段大于60才会被显示出来.
  • 外链接:

    左外连接(LEFT OUTER JOIN与右外连接(RIGHT OUTER JOIN)

    select * from student left(或者right) JOIN sc on ( student.sno =sc.sno and sc.SCORE>60);

    表示除了符合连接中匹配条件的数据还要显示剩下左右的左表或者右表的记录.

  • 自然连接:

    natural join:

    指自动将表中相同名称的列进行记录匹配。

    select course.,sc. from course natural join sc

  • 自连接:

    使用表的别名进行表自身的连接:

    select * from sc a,sc b where a.CNO =b.cno and a.SCORE>60;

联合查询:

将多次查询(多条select语句), 在记录上进行拼接(字段不会增加)

每一条select语句获取的字段数必须严格一致

关键字 union

选项: all 保留所有数据,distinct 去重.

语法: select field from table union [option] tablt2

实例:select * from student union distinct select * from student;
posted @ 2018-06-21 15:04  CurryRice  阅读(111)  评论(0编辑  收藏  举报