数据库基础知识——DQL语言(二)
生活随笔
收集整理的這篇文章主要介紹了
数据库基础知识——DQL语言(二)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1.多表連接查詢
- 1.1 sql92語法
- 1.1.1 等值連接
- 1.1.2 sql92:非等值連接
- 1.1.3 sql92:自連接
- 1.2 sql99語法
- 1.2.1 等值連接
- 1.2.2 非等值連接
- 1.2.3 內(nèi)連接
- 1.2.4 左右連接(外連接)
- 2.子查詢
- 2.1 where或者having后面
- 3.分頁查詢
- 4.聯(lián)合查詢
1.多表連接查詢
笛卡爾乘積:表1,有m行;表2,有n行,如果連接條件省略或無效,則結(jié)果=m*n; 解決辦法:添加上連接條件1.1 sql92語法
sql92語法的多表連接查詢包含:等值連接、非等值連接、自連接1.1.1 等值連接
1.等值連接的結(jié)果 = 多個表的交集 2.n表連接,至少需要n-1個連接條件 3.多個表不分主次,沒有順序要求 4.一般為表起別名,提高閱讀性和性能 #將beauty表中的女生和boys表中的男生匹配 select name,boyName from beauty,boys where beauty.boyfriend_id=boys.id;#查詢員工名,員工工種id和員工工種名 select last_name,employees.job_id,job_title from employees,jobs where employees.job_id=jobs.job_id;#查詢員工名和對應(yīng)的部門 select last_name,department_name from employees,departments where employees.department_id=departments.department_id;#查詢有獎金的員工名和部門名 select last_name,department_name from employees,departments where employees.department_id=departments.department_id and employees.commission_pct is not null;#查詢每個城市的部門個數(shù) select count(*),city from locations,departments where locations.location_id=departments.location_id group by city;#查詢員工名,部門名和所在的城市 select last_name,departments.department_name,locations.city from employees,departments,locations where employees.department_id=departments.department_id and departments.location_id=locations.location_id;1.1.2 sql92:非等值連接
#查詢員工名,員工的工資和工資級別 select last_name,salary,grade_level from employees,job_grades where salary between lowest_sal and highest_sal;1.1.3 sql92:自連接
#查詢員工名和上級姓名 select e.last_name,e.employee_id,l.last_name as "leader" from employees e,employees l where e.manager_id=l.employee_id;1.2 sql99語法
含義:1999年推出的sql語法 支持: 等值連接、非等值連接 (內(nèi)連接) 外連接 交叉連接語法:select 字段,... from 表1 【inner|left outer|right outer|cross】join 表2 on 連接條件 【inner|left outer|right outer|cross】join 表3 on 連接條件 【where 篩選條件】 【group by 分組字段】 【having 分組后的篩選條件】 【order by 排序的字段或表達式】好處:語句上,連接條件和篩選條件實現(xiàn)了分離,簡潔明了!1.2.1 等值連接
#查詢員工名和部門名 select last_name,department_name from employees inner join departments on employees.department_id=departments.department_id;#查詢名字中包含e的員工名和工種名(包含篩選) select last_name,job_title from employees inner join jobs on employees.job_id=jobs.job_id where last_name like "%e%";#查詢部門個數(shù)大于3的城市和部門個數(shù) select city,count(*) from locations inner join departments on locations.location_id=departments.location_id group by city having count(*)>3;1.2.2 非等值連接
#查詢員工名和工資和工資水平 select last_name,salary,grade_level from employees inner join job_grades on employees.salary between job_grades.lowest_sal and job_grades.highest_sal;1.2.3 內(nèi)連接
#查詢員工的名字和上級姓名 select e.last_name,l.last_name as "leader" from employees as e inner join employees as l on e.manager_id=l.employee_id;1.2.4 左右連接(外連接)
用于查詢一個表中有,另外一個表中沒有的記錄 特點:外連接的查詢結(jié)果為主表中的所有記錄,如果從表中有和主表匹配的,則顯示對應(yīng)的值,否則顯示為null;左外連接:left join的左邊是主表;右外連接:right join的右邊是主表; #查詢女神的男朋友,要是沒有,顯示null select name,boyName from beauty left join boys on beauty.boyfriend_id=boys.id;2.子查詢
含義:
一條查詢語句中又嵌套了另一條完整的select語句,其中被嵌套的select語句,稱為子查詢或內(nèi)查詢 在外面的查詢語句,稱為主查詢或外查詢特點:
1、子查詢都放在小括號內(nèi) 2、子查詢可以放在from后面、select后面、where后面、having后面,但一般放在條件的右側(cè)select后面:僅僅支持標量子查詢from后面:支持表子查詢where或者having后面:標量子查詢,列子查詢,行子查詢exists后面:表子查詢 3、子查詢優(yōu)先于主查詢執(zhí)行,主查詢使用了子查詢的執(zhí)行結(jié)果 4、子查詢根據(jù)查詢結(jié)果的行數(shù)不同分為以下兩類: ① 單行子查詢結(jié)果集只有一行一般搭配單行操作符使用:> < = <> >= <= 非法使用子查詢的情況:a、子查詢的結(jié)果為一組值b、子查詢的結(jié)果為空② 多行子查詢結(jié)果集有多行一般搭配多行操作符使用:any、all、in、not inin: 屬于子查詢結(jié)果中的任意一個就行any和all往往可以用其他查詢代替2.1 where或者having后面
標量子查詢(單行子查詢) 列子查詢(多行子查詢) 行子查詢(多列多行)特點: 子查詢一般放在小括號內(nèi); 子查詢一般放在條件的右側(cè); 標量子查詢,一般搭配著單行操作符使用>< >= <= = <> 列子查詢一般搭配著多行操作符使用in、any、some、all #查詢比Abel工資高的員工名和工資 select last_name,salary from employees where salary>(select salary from employees where last_name="Abel" );#查詢job_id與141號員工相同,salary比143號員工多的員工的姓名,job_id和salary select last_name,job_id,salary from employees where job_id=(select job_idfrom employeeswhere employee_id=141 ) and salary>(select salaryfrom employeeswhere employee_id=143 );#查詢最低工資大于50號部門最低工資的部門id和工資 select department_id,min(salary) from employees group by department_id having min(salary)>(select min(salary)from employeeswhere department_id=50 );#查詢location_id是1400或者1700的部門中的所有員工的姓名 select last_name from employees where department_id in (select department_idfrom departmentswhere location_id in (1400,1700) );3.分頁查詢
應(yīng)用場景:
實際的web項目中需要根據(jù)用戶的需求提交對應(yīng)的分頁查詢的sql語句語法:
select 字段|表達式,... from 表 【where 條件】 【group by 分組字段】 【having 條件】 【order by 排序的字段】 limit 【起始的條目索引,】條目數(shù);特點:
1.起始條目索引從0開始2.limit子句放在查詢語句的最后3.公式:select * from 表 limit (page-1)*sizePerPage,sizePerPage 假如: 每頁顯示條目數(shù)sizePerPage 要顯示的頁數(shù) page #查詢前五條員工信息 select * from employees limit 0,5;#查詢第11-25條 select * from employees limit 10,25;#查詢有獎金的,工資前十名的員工姓名 select last_name,salary from employees where commission_pct is not null order by salary desc limit 10;4.聯(lián)合查詢
引入:
union 聯(lián)合、合并
語法:
select 字段|常量|表達式|函數(shù) 【from 表】 【where 條件】 union 【all】 select 字段|常量|表達式|函數(shù) 【from 表】 【where 條件】 union 【all】 select 字段|常量|表達式|函數(shù) 【from 表】 【where 條件】 union 【all】 ..... select 字段|常量|表達式|函數(shù) 【from 表】 【where 條件】特點:
1、多條查詢語句的查詢的列數(shù)必須是一致的 2、多條查詢語句的查詢的列的類型幾乎相同 3、union代表去重,union all代表不去重 #查詢員工的名字中,郵箱中含有a,并且部門號大于90的員工信息 select * from employees where last_name like "%a%" union select * from employees where email like "%a%" union select * from employees where department_id>90;總結(jié)
以上是生活随笔為你收集整理的数据库基础知识——DQL语言(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机系统结构 期末复习
- 下一篇: 基于交换机的PC端网络通信