Connection (multi-table) query of database principle (3) (day 16)

Join query is the most important query in relational data, and can query two or more tables.

Type of connection:

    Inner joins: use symbolic joins like "=, >, <"

    Outer join: only limit to one table

        Left outer join: no restrictions on the table on the left side of the join

        Right outer join: no restrictions on the table on the right side of the join

        Full outer join: no restrictions on both tables

    Cross join: Perform Cartesian set operations on two tables to form a new result table, the number of rows in the result table is equal to the product of the number of rows in the two tables

here comes the point

First, the realization of the connection query

    1. Use the FROM clause and the WHERE clause to implement multi-table queries

        (1). Use the FROM clause to directly implement the connection of the table 

SELECT [student].s_id,[Student].s_name,[Grade].c_grade FROM [Student],[Grade] ORDER BY [Student].s_id


            (2). Use the WHERE clause to achieve conditional joins

SELECT [student].s_id,[Student].s_name,[Grade].c_grade FROM [Student],[Grade] WHERE [Student].s_id = [Grade].s_id


            (3). Use the WHERE clause to achieve self-connection

SELECT g.* FROM [Student] s ,[Grade] g WHERE s.s_name = '张三' AND s.s_id = g.s_id


    2. Use the JOIN keyword to implement multi-table query

        (1). Use [INSERT]JOIN to implement inner join query

SELECT [Student].s_id,[Student].s_name,[Grade].c_grade
FROM [Student] INNER JOIN [Grade]
ON [Student].s_id = [Grade].s_id


        (2). Use LEFT[OUTER]JOIN to implement left outer join query

SELECT [Student].s_id,s_name,c_grade
FROM [Student] LEFT OUTER JOIN [Grade]
ON [Student].s_id = [Grade].s_id


        (3). Use RIGHT[OUTER]JOIN to implement right outer join query

SELECT [Grade].c_id,[Grade].s_id,c_grade
FROM [Student] RIGHT OUTER JOIN [Grade]
ON [Student].s_id = [Grade].s_id


        (4). Use FULL[OUTER]JOIN to achieve full outer join query


        (5). Use CROSS JOIN to implement cross join query

     

I feel that there is a problem, please help to see what is wrong

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723159&siteId=291194637