デカルト積の理解
select id,department_name from employees,departments;#错的 select id,department_id,department_name from employees CROSS JOIN departments;#错的
各従業員と各部門が 1 回照合されました (見つかったエントリの数 = ID の数 * 数値
エラーの原因: 接続条件がありません
接続条件を書き込みます: 表 1. 列 = 表 2. 列 (複数接続する場合)テーブル、少なくとも n-1 個の接続条件を使用する必要があります)
select id,employees.name,department_name from employees,departments WHERE employees.name = departments.name;
注: 表示される列がクエリ対象のテーブル内で同じ名前を持つ場合は、それがどのテーブルからのものであるかを示す必要があります。 :employees .name
複数のテーブルをクエリするときにどのテーブル情報を表示するかを指定することをお勧めします (最適化)
最適化: FROM の後にテーブルの別名を使用できますが、別名は一度使用します。必ずエイリアスを使用してください。
等価結合:上記 =
select t1.id,t1.name,t2.grade from employees t1,departments t2 WHERE ti.salary BETWEEN t2.lowest_salary AND t2.highest_salary ;#非等值
#显示员工(t1)和其管理者(t2)的基本信息 select t1.id,t1.name,t2.id,t2.name from employees t1,employees t2#一个表看作两个表 WHERE t1.manage_id = t2.id ;#自连接
WHERE t1.department_id = t2.department_id(+)#左连接
select t1.id,t1.name,t2.department_name,t3.environment from employees t1 JOIN departments t2 ON t1.department_id = t2.department_id JOIN locations t3#加入第二个人表 ON t2.department_location = t3.department_location;
## を使用します
select t1.name,t2.department_name#左外连接 from employees t1 LEFT OUTER(可省略) JOIN departments t2 ON t1.department_id = t2.department_id;
SELECT colum... FROM table1 UNION (ALL) SELECT colum... FROM table2
7 種類の SQL JOINS の実装
中央の図 (内部結合):
select t1.name,t2.department_name from employees t1 JOIN departments t2 ON t1.department_id = t2.department_id;
左上の図 (左外部結合):
select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id;
右上の図 (右外部結合):
select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id;
中左の図:
select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t2.department_id IS NULL;
中右図:
select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t1.department_id IS NULL;
左下の図 (完全外部結合):
#方式一:左上图 UNION ALL 右中图 select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id UNION ALL select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t1.department_id IS NULL; #方式二:左中图 UNION ALL 右上图 select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t2.department_id IS NULL UNION ALL select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id;
右下の図:
#左中图 UNION ALL 右中图 select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t2.department_id IS NULL UNION ALL select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t1.department_id IS NULL;
SQL 構文の新機能
USING 接続 (自己結合には適用されません)
select t1.name,t2.department_name from employees t1 JOIN departments t2 ON t1.department_id = t2.department_id; 等价于 select t1.name,t2.department_name from employees t1 JOIN departments t2 USING(department_id);
以上がMySQL マルチテーブルクエリのケース分析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。