Detailed JOIN usage of mysql - with query examples

Detailed JOIN usage of mysql - with query examples

In SQL, JOIN
is an operation used to join data from multiple tables together. It combines qualified rows in two or more tables by specifying join conditions to produce a new result set.

Common JOIN types in SQL include INNER JOIN , LEFT JOIN , RIGHT JOIN , and FULL OUTER JOIN .
The usage of these JOIN types is detailed below:

1.INNER JOIN (inner connection): It returns the rows in the two tables that satisfy the join condition. It only returns rows from both tables that satisfy the condition in common, and does not include non-matching rows. The syntax is as follows:

SELECT 列名 FROM1 INNER JOIN2 ON 连接条件;

2.LEFT JOIN (left connection): It returns all the rows in the left table, and the rows in the right table that satisfy the join condition. If there are no matching rows in the right table, a NULL value is returned. The syntax is as follows:

SELECT 列名 FROM1 LEFT JOIN2 ON 连接条件;

3.RIGHT JOIN (right connection): It returns all the rows in the right table, and the rows in the left table that satisfy the join condition. If there are no matching rows in the left table, a NULL value is returned. The syntax is as follows:

SELECT 列名 FROM1 RIGHT JOIN2 ON 连接条件;

4.FULL OUTER JOIN (full outer connection): It returns all the rows in the two tables, regardless of whether the connection conditions are met. If there are no matching rows in a table, a NULL value is returned. The syntax is as follows:

SELECT 列名 FROM1 FULL OUTER JOIN2 ON 连接条件;

In connection conditions, you can use comparison operators (such as =, <>, <, >) to specify the conditions of the connection. A join condition can be an equality condition for one column, or a combination condition for multiple columns.
It should be noted that the JOIN operation may increase the number of rows in the result set, so it is necessary to select the appropriate JOIN type according to the specific business requirements.

Attach a piece of sample code actually used by the project:

<select id="getPointDetail" resultType="com.****************">
        SELECT
        hp.bk_id zdbh,
        spb.lng,
        spb.lat,
        spb.bdrq,
        spb.xzq,
        spb.ydlx,
        spb.ssdzdy as swdzdy,
        a.value as wljcdwlxmc,
        b.value as jcjlxmc,
        c.value as jcjjbmc,
        d.value as dxsllxmc,
        e.value as hsclxmc,
        f.value as ydlxmc,
        sgnp.*
        FROM hub_point hp
        LEFT JOIN sat_groundwater_network_point sgnp ON hp.hid = sgnp.hid
        left join sat_point_base spb on spb.hid = hp.hid
        left join (select * from sys_dict_item where fk_sort_code = '1019') a on sgnp.wljcdwlx = a.code
        left join (select * from sys_dict_item where fk_sort_code = '1017') b on sgnp.jcjlx = b.code
        left join (select * from sys_dict_item where fk_sort_code = '1016') c on sgnp.jcjjb = c.code
        left join (select * from sys_dict_item where fk_sort_code = '1013') d on sgnp.dxsllx = d.code
        left join (select * from sys_dict_item where fk_sort_code = '1012') e on sgnp.hsclx = e.code
        left join (select * from sys_dict_item where fk_sort_code = '1001') f on spb.ydlx = f.code
        <where>
            <if test="id != null and id != ''">
                hp.hid = #{
    
    id}
            </if>
        </where>
        ORDER BY sgnp.load_dts DESC
        LIMIT 1
    </select>

This sample code is a SQL query statement mapped by the maper of the springboot project.
The code is explained line by line as follows:

  1. Line 1: The tag defines a query statement, and the id attribute is "getPointDetail".
  2. Lines 2 to 20: This is the actual query statement. In these lines, multiple columns are selected as the result set through the SELECT keyword, and the table is specified through the FROM keyword. The query statement returns a result set that includes
    selected columns from the hub_point table, sat_groundwater_network_point table, and sat_point_base table.
  3. Lines 7 to 19: These are multiple LEFT JOIN clauses that join other tables with the hub_point table. Specify the join condition with the ON clause. Each
    LEFT JOIN clause connects a subquery and a table. The subquery is a query that selects specific fields from the sys_dict_item table,
    filtered by the WHERE clause and specific conditions.
  4. Lines 22 to 24: Labels are used to wrap WHERE clauses to add additional conditions. Here,
    the condition is judged by the tag, if the parameter id is not empty and not an empty string, add hp.hid = #{id} as a condition to the WHERE clause.
  5. Line 26: Sort in descending order by the sgnp.load_dts column through the ORDER BY clause.
  6. Line 27: Use the LIMIT clause to limit the number of returned rows in the result set to 1.
  7. The above is a brief explanation of this code. It queries multiple tables and uses multiple LEFT JOINs to join the tables. At the same time, it also uses the WHERE
    clause, ORDER BY clause and LIMIT clause to further filter and sort the result set.

Guess you like

Origin blog.csdn.net/qq_61950936/article/details/131475806