【数据库】MySQL 学习笔记之二:查询语句

第一:select 语句包含的对象

select  表中存在的列、内置函数【upper(),round()等】、逻辑运算【+-*/等】、新加列【‘’单引号内引用名】 from  

表名;

select emp_id ,'ACTIVE',emp_id * 3.14,upper(lname) from `employee`;

注意:from 表名;语句可以省略,例如:执行一个内建函数,或者简单表达事

select user(),database();

第二:列的别名【as 关键字】

意思:原本表中列名含义模糊,或者对操作的描述

select emp_id ,'ACTIVE' as status,
emp_id *3.14 as empid_x_pi,
upper(lname) as last_name_upper
from `employee`;

怎么样实现别名:在原始表名后面加加上关键字as即可;新加入的列,也同样操作,比如楼上的‘ACTIVE' as status 语句。

注意:关键字在mysql命令行中可以省略。

第三:去除重复行【distinct 关键字】

select distinct cust_id from `account`;

注意:该关键字distinct 消耗较多资源,因此不建议随意使用

第四:from 语句

from语句定义了查询中所使用的表,以及链接这些表的方式。

宽泛定义下,存在三种类型的表:

1、永久表   create table 语句创建

2、临时表   select 子查询语句返回的表

3、虚拟表  使用create view 子句所创建的视图

a、子查询语句产生的表【相当于嵌套select语句】

语法:select  别名.列名1,别名.列名2,别名.列名3      from (select  列名1,列名2,列名3,列名4,,from 表名)as 别名;

select e.emp_id,e.fname,e.lname from (select emp_id,fname,lname,start_date,title from 
`employee`) as e;

外围查询需要通过别名来引用   子查询;

b、视图

视图:实际上不拥有任何数据,只是行为表现的像一个表,针对视图的查询,实际上针对视图定义的查询。

1、定义视图

create view  employee_vw  as
select emp_id,fname,lname,YEAR(start_date) as start_year from `employee`;

语法 :create view  视图名(列1,列2,,,)  as    select  。。from 表名;

如果  视图名(列1,列2,,,)中没定义括号的列名,那么直接引用子查询的列名。

以上语句,对原始表start_date语句应用了个取年份的year函数,并且设置了 start_year的别名。

2、查询视图

select emp_id,start_year from employee_vw;

查询操作时,可以直接把视图当作表来处理。

c、from 子句 表连接

如果 from 语句中出现了多个表,那么语句要包含各表之间的连接条件。

语法:select  表1.列1,表1.列2  ,,,表2.列1 as 别名       from 

          表1  INNER JOIN 表2  

          ON 表1.某列=表2.某列;

1、语法中表1,表2既可以引用全名,如下

select employee.emp_id ,employee.fname,
employee.lname,department.name as dept_name 
from employee INNER JOIN department ON employee.dept_id = department.dept_id;

2、也可以在from 语句中定义别名

select e.emp_id,e.fname,e.lname,d.name as dept_name
from `employee` as e inner join `department` as d on e.dept_id = d.dept_id;

连接两张表的机制,此处时join方式;

两张表的连接条件由ON 语句指定,本例是 on employee.dept_id = department.dept_id确定。

第五:where 语句

 提取符合条件的行,过滤不需要的行。

1、单个过滤条件

select * from `employee` where title='Teller';

2、多个过滤条件,即通过与或非语句   定义  过滤条件。

select * from `employee` where title='Teller' and start_date > '2006-1-1';

3、同时使用and or操作符,则需要使用圆括号()

select * from `employee` where (title='Head Teller' and start_date >'2006-1-1') or (title='Teller' and start_date >'2007-1-1');

混合使用不同操作符,需要用圆括号来分隔成组条件。

第六:group by 语句 having子句

之前语句只是提出数据,并没有做任何加工,本节语句能够以where语句类似的方式对分组数据进行加工过滤。

select d.name,count(e.emp_id) as num_employee from
`department` as d inner join `employee` as e on d.dept_id = e.dept_id
group by d.name having count(e.emp_id)>2;

分组计算各部门雇员人数,并且只提取雇员大于2的部门。

group by  条件,以某种条件分组,having 子句,则对分组条件,进一步限制。

第七:order by 子句

1、对原始列数据/根据列数据计算的表达式结果,进行排序。

select open_emp_id,product_cd from account order by open_emp_id,product_cd;

结果,先按照open_emp_id排序,在按照product_cd排序。默认的都是升序。

2、关键字:desc 降序,asc升序。

select open_emp_id,product_cd from account order by open_emp_id desc ,product_cd desc; 

3、根据表达式进行排序

排序的条件并没有出现在查询内容中

select * from `customer` order by right(fed_id,3);

根据fed_id列是值,应用right()函数,取最后3位数字进行排序。

4、根据占位符排序

如果根据select 子句中的列来排序可以用占位符。

注意:如果是用表中其他的列或者表达式,肯定就不能应用占位符。

select emp_id,title,start_date,fname,lname
from `employee`
order by 2,5;


猜你喜欢

转载自blog.csdn.net/Jesszen/article/details/80766075