单表查询多表查询和子查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单表查询多表查询和子查询相关的知识,希望对你有一定的参考价值。
查询语句:
select SELECT-LIST from TB where QYAKUDUCATION;
简单查询:
select * from TB_NAME;
select FIED1,FIED2 from TB_NAME; 投影
select [distinct] * from TB_NAME where QYAKUDUCATION;选择 #distinct重复的值只显示一次
from子句:要查询的关系 表,多个表,其他的select语句
where子句:指定布尔关系表达式,
= , > , >= , <= , <
<> 不等于
<=> 完全不等于,NULL值也可以进行比较
也可以使用算数表达式
mysql> select NAME from students where AGE+1>20
与或非关系:
and &&
or ||
not !
xor 异或
between ... and ... 在谁和谁之间
like ‘‘ 比较操作
%:任意长度。任意字符
_:任意单个字符
regexp或者 rlike支持正则表达式
in ( ) 列表
is null 判断是否为空
is not null 判断是否不空
where条件应用:
mysql> select NAME from students where AGE>20 and GENDER=‘M‘;
mysql> select NAME from students where AGE>20 or GENDER=‘M‘;
mysql> select NAME from students where not AGE>20;
查询年龄不大于20并且不为男性的人
mysql> select NAME from students where not AGE>20 and not GENDER=‘M‘;
mysql> select NAME from students where not (AGE>20 or GENDER=‘M‘);
查询年龄20-25之间的所有人
mysql> select NAME from students where AGE>=20 and AGE<=25;
mysql> select NAME from students where AGE between 20 and 25;
查询名字为Y开头的所有人
mysql> select NAME from students where NAME like ‘Y%‘;
查询Y开头。后面跟四个字符的人名
mysql> select NAME from students where NAME like ‘Y____‘;
查询整个名字中 包含 ing字符串的人名
mysql> select NAME from students where NAME like ‘%ing%‘;
查询用户名称中以大写字母M N Y开头的
mysql> select NAME from students where NAME like ‘M%‘ or NAME like ‘N%‘ or NAME like ‘Y%‘;
mysql> select NAME from students where NAME rlike ‘^[MNY].*$‘;
查询年龄为18 20 25的姓名
mysql> select NAME from students where AGE=18 or AGE=20 or AGE=25;
mysql> select NAME from students where AGE in (18,20,25);
查询CID2为空的
mysql> select NAME from students where CID2 is NULL;
查询CID2不空的
mysql> select NAME from students where CID2 is NOT NULL;
排序:order by FIELD_NAME {ASC|DESC} 默认是升序
根据姓名进行排序:升序
mysql> select NAME from students where CID2 is NOT NULL order by NAME;
降序
mysql> select NAME from students where CID2 is NOT NULL order by NAME desc;
给字段取别名:AS
mysql> select NAME AS Student_Name from students;
直接进行算数运算
mysql> select 2+1 AS SUM;
+-----+
| SUM |
+-----+
| 3 |
+-----+
1 row in set (0.00 sec)
limit N: 只显示查询到的前N个
limit N,X: 查询到的前N个不显示。从N+1个开始显示,共显示X个
mysql> select NAME AS Student_Name from students limit 2;
mysql> select NAME AS Student_Name from students limit 2,3;
聚合计算:
avg()平均数
max()
min()
sum()
count() 一共有多少个
求所有同学的平均年龄
mysql>select avg(AGE) from students;
查找年龄最大的同学
mysql>select max(AGE) from students;
查找年龄最小的同学
mysql>select min(AGE) from students;
查找年龄之和
mysql>select sum(AGE) from students;
查找一共有多少人
mysql>select count(AGE) from students;
group by 分组:
having QUALIFICATION 过滤条件
查找男同学和女同学的平均年龄
mysql>select AGE,Gender from students group by Gender;
mysql>select avg(AGE) from students group by Gender;
统计选择CID1课程的同学的个数
mysql>select count(CID1) AS Persons,CID1 from students group by CID1;
显示哪些课程人数大于2个人的 这里必须用having而不能使用where
mysql>select count(CID1) AS persons,CID1 from students group by CID1 having Persons>=2;
多表查询:
连接:
交叉连接: 笛卡尔乘积
自然连接:仅保留具有等值关系的
外连接:用于显示查询包含NULL
左外连接: ...left join ...on...
又外连接: ... right join ...on ...
自连接:
自然连接:
mysql>select students.NAME,courses.CNAME from students,courses where students.CID1 = courses.CID;
mysql>select s.NAME,c.CNAME from students AS s,courses AS c where s.CID1 = c.CID;
外连接:
显示同学对应的课程,有人选就显示有。没有人选就显示人物对应的课程为NULL
select s.NAME, c.CNAME from students as s left join as c on s.CID1=c.CID;
显示同学对应的课程,有人选就显示。没有人选就显示课程对应的人名为NULL
select s.NAME, c.CNAME from students as s right join as c on s.CID1=c.CID;
自连接:
select c.NAME as teacher, s.NAME as stu from stufents as s,students as c where c.TID=s.CID;
子查询:
比较操作中使用子查询:子查询只能返回一个单值
in():使用子查询
在from中使用子查询
联合查询:
union
DISTINCT:选择唯一的结果集
找students表中年龄大雨平均年龄的同学
select NAME from students where AGE > (select avg(AGE) from students);
找到同名和老师年龄一样的
select NAME from students where AGE in (select AGE from tutors );
在from中使用子查询
select NAME ,AGE from (select NAME,AGE from students) as t where t.AGE >=20;
union把2个查询语句的结果联合起来
(select NAME,AGE from students )union (select TNAME ,AGE from tutors);
找出CID2中没有同学选用的课程号对应的课程名
select Cname,CID2 from ( select c.Cname,s.CID2 from students as s , courses as c where s.TID=c.TID ) as d where d.CID2 is NULL;
挑出没有教授任何课程的老师,每个老师及其所教授课程的对应关系在courses表中
select Tname from tutors where TID not in ( select distinct TID from courses );
找出students表中CID1有两个或两个以上同学学习了的同一个课程的课程名称
select Cname from courses where CID in (select CID1 from students group by CID1 having count (CID1) >=2);
视图:存储下来的select语句
基于基表的查询视图
view
mysql> help create view
CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
create view sct as select NAME from students where AGE in (select AGE from tutors );
show tables会发现select的变成了一张表
select * from sct;
mysql> help drop view
DROP VIEW [IF EXISTS]
view_name [, view_name] ...
[RESTRICT | CASCADE]
show create view sct\G 可以显示创建sct视图所使用的完整语句
show create table crouses; 查看创建crouses表的完整语句
# mysql -e ‘create database edb;‘可以直接往mysql里创建数据库
本文出自 “运维成长路” 博客,谢绝转载!
以上是关于单表查询多表查询和子查询的主要内容,如果未能解决你的问题,请参考以下文章