#多表查詢
#出現了笛卡爾積的錯誤
select employee_id,last_name,department_name
from employees,departments; #2889
select 2889 / 107
from dual; # 27
select department_id
from departments; #27
#正確的:
select employee_id,last_name,department_name
from employees,departments
#多表的連線條件
where employees.`department_id` = departments.`department_id`;
#如果查詢的字段在多個表**現,則必須顯式的指明此欄位所屬的表
#sql優化
select employees.employee_id,employees.last_name,departments.department_name,employees.department_id
from employees,departments
where employees.`department_id` = departments.`department_id`;
#表的別名,可以在select、where中使用。
select e.employee_id,e.last_name,d.department_name,e.department_id
from employees e,departments d
where e.`department_id` = d.`department_id`;
#三個表的連線練習:
select employee_id,last_name,department_name,city
from employees e,departments d,locations l
where e.`department_id` = d.`department_id`
and d.`location_id` = l.`location_id`;
#結論:n個表做查詢操作,則至少有 n - 1個連線條件。
#################################
/*多表查詢的總結:分類
等值連線 vs 非等值連線
自連線 vs 非自連線
內連線 vs 外連線
*/#非等值連線
select employee_id,last_name,salary,grade_level
from employees e,job_grades j
where e.`salary` between j.`lowest_sal` and j.`highest_sal`;
#自連線
select emp.employee_id,emp.last_name,mgr.employee_id,mgr.last_name
from employees emp,employees mgr
where emp.`manager_id` = mgr.`employee_id`;
#內連線:合併具有同一列的兩個以上的表的行, 結果集中不包含乙個表與另乙個表不匹配的行
select e.employee_id,e.last_name,d.department_name,e.department_id
from employees e,departments d
where e.`department_id` = d.`department_id`; #106行
#左外連線:除了查詢到兩個表滿足連線條件的行以外,還返回左表中不滿足條件的行
#右外連線:除了查詢到兩個表滿足連線條件的行以外,還返回右表中不滿足條件的行
#字眼:查詢所有的。。。。
#sql 99語法中提到了如何使用左外連線、右外連線
#sql 99語法實現內連線:
select e.employee_id,e.last_name,d.department_name,e.department_id
from employees e join departments d
on e.`department_id` = d.`department_id`;
select e.employee_id,e.last_name,d.department_name,e.department_id,city
from employees e join departments d
on e.`department_id` = d.`department_id`
join locations l
on d.`location_id` = l.`location_id`;
#sql 99語法實現左外連線:
select e.employee_id,e.last_name,d.department_name,e.department_id
from employees e left outer join departments d
on e.`department_id` = d.`department_id`;
#sql 99語法實現右外連線:
select e.employee_id,e.last_name,d.department_name,d.department_id
from employees e right join departments d
on e.`department_id` = d.`department_id`;
# 滿外連線:mysql不支援full join
select e.employee_id,e.last_name,d.department_name,d.department_id
from employees e full join departments d
on e.`department_id` = d.`department_id`;
mysql 的各種檔案詳細說明
1 資料目錄 mysql show global variables like datadir variable name value datadir home mysqldata 1 row in set 0.00 sec innodb資料檔案。儲存innodb資料,索引,double write...
mysql中insert into語句的多種用法
mysql資料庫對於插入資料有專門的語句,就是insert into,mysql中insert語句是最常用的插入語句,insert 語句可以用來將一行或多行資料插到資料庫表,mysql中insert into語句有固定的語法格式,卻因引數的不同有不一樣的用法,本文向大家介紹mysql中insert ...
mysql5 7最詳細的安裝過程
原文 安裝mysql5.7前的準備 service firewalld stop 關閉防火牆 配置yum阿里雲源 mv etc yum.repos.d centos base.repo etc yum.repos.d centos base.repo.backup curl o etc yum.re...