Multi-table join query on SQL statement

The attachment is the table building script for mysql, mssqlserver and oracle

# Multi-table join query

# SQL92
# Cartesian Product
select * from student_table s, teacher_table t;
# Equijoin =
select * from student_table s, teacher_table t where s.java_teacher = t.teacher_id;
# non-equijoin > <
select * from student_table s, teacher_table t where s.java_teacher > t.teacher_id;
select * from student_table s, teacher_table t where s.java_teacher < t.teacher_id;
# Multi-conditional join
select * from student_table s, teacher_table t where s.java_teacher = t.teacher_id and s.student_id > 1 and t.teacher_id < 2;
# outer join
# (MySql and MSSQLServer do not support, use Oracle test) left outer join (display the rows that meet the connection conditions, and connect the rows that do not meet the conditions in the right table with all the fictitious rows with null values ​​in the left table)
select * from student_table s, teacher_table t where s.java_teacher(+) = t.teacher_id;
# (MySql and MSSQLServer do not support, use Oracle test) Right outer join (display the rows that meet the connection conditions, and connect the rows that do not meet the conditions in the left table with all the fictitious rows with null values ​​in the right table)
select * from student_table s, teacher_table t where s.java_teacher = t.teacher_id(+);

# SQL99
# Cross join (Cartesian product in SQL92) cross join
select * from student_table s cross join teacher_table t;
# (MSSQLServer does not support, MySql and Oracle support) natural join (column connection with the same name) natural join, if there is no column with the same name, it is a cross join
select * from student_table s natural join teacher_table t;
# (MSSQLServer does not support, MySql and Oracle support) Use the using clause to specify multiple column connections with the same name (if there is no attribute with the same name, an error will be reported)
select * from student_table s join teacher_table t using (teacher_name);
# Equijoin using join...on clause =
select * from student_table s join teacher_table t on s.java_teacher = t.teacher_id;
# Use join...on clause for non-equijoin > <
select * from student_table s join teacher_table t on s.java_teacher > t.teacher_id;
select * from student_table s join teacher_table t on s.java_teacher < t.teacher_id;
# Use join...on clause for multi-condition join
select * from student_table s join teacher_table t on s.java_teacher = t.teacher_id and s.student_id > 1 and t.teacher_id < 2;
# Left outer join left joinj...on is the opposite of the SQL92 standard (displays the rows that meet the join conditions, and joins the rows of the left table that do not meet the conditions with all the fictitious rows with null values ​​in the right table)
select * from student_table s left join teacher_table t on s.java_teacher = t.teacher_id;
# Right outer join right join...on is the opposite of the SQL92 standard (displays the rows that meet the join conditions, and joins the rows that do not meet the conditions on the right table with all the fictitious rows with null values ​​from the left table)
select * from student_table s right join teacher_table t on s.java_teacher = t.teacher_id;
# (MySql does not support, MSSQLServer and Oracle support) full outer join full join...on (display the rows that meet the connection conditions, and connect the rows that do not meet the conditions in the left table with all the fictitious rows with null values ​​in the right table , at the same time connect the rows that do not satisfy the condition in the right table with all the imaginary rows with null values ​​in the left table)
select * from student_table s full join teacher_table t on s.java_teacher = t.teacher_id;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326754928&siteId=291194637