当前位置:网站首页>Mysql database knowledge points (III)

Mysql database knowledge points (III)

2022-06-22 19:53:00 zhulin1028

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 25 God , Click to see the event details

One 、 The joint query

UNION The operator can add two or more to SELECT The query result set of the statement is combined into a result set to display , That is, the union query is executed .UNION The grammar format of is :

select_statement UNION [ALL] selectstatement

[UNION [ALL] selectstatement][…n] among selectstatement For... To be united SELECT Query statement .

ALL Option means to merge all rows into the result set . When this item is not specified , Only one row is reserved for duplicate rows in the federated query result set .

Joint query , The column title of the query result is the column title of the first query statement . therefore , To define column headings, you must define... In the first query statement . When sorting union query results , You must also use the column names in the first query statement 、 Column header or column sequence number .

In the use of UNION Operator , Ensure that there are the same number of expressions in the selection list of each joint query statement , And each query selection expression should have the same data type , Or they can be automatically converted to the same data type . During automatic conversion , For numeric types , The system converts low-precision data types into high-precision data types .

Where multiple queries are included UNION In the sentence , The execution sequence is from left to right , Use parentheses to change this execution order . for example : Inquire about 1 UNION ( Inquire about 2 UNION Inquire about 3)

Two 、 Link query

        Multiple table queries can be implemented through join operators . Connection is the main feature of relational database model , It is also a symbol that distinguishes it from other types of database management systems .

       In relational database management system , When creating a table, the relationship between the data does not have to be determined , All the information of an entity is often stored in one table . When retrieving data , Query the information of different entities stored in multiple tables through connection operation . Connection operation brings great flexibility to users , They can add new data types at any time . Create new tables for different entities , Then query by connection .

       You can connect at SELECT Of the statement FROM Clause or WHERE Clause , Specious in FROM Clause helps to link the join operation to WHERE The search conditions in the clause are distinguished . therefore , stay Transact-SQL This method is recommended in .SQL-92 The standard defines FROM The join syntax format of clause is : 

FROM join_table join_type join_table [ON (join_condition)]

among join_table Indicates the name of the table participating in the join operation , Join can operate on the same table , You can also operate on multiple tables , A join to the same table operation is also called a self join .join_type Point out the connection type , It can be divided into three kinds : Internal connection 、 External connections and cross connections .

      Internal connection (INNER JOIN) Use the comparison operator to perform a comparison between tables ( some ) Comparison of column data , And list the data rows in these tables that match the connection conditions . Depending on the comparison method used , Internal connection is divided into equivalent connection 、 Natural connection and unequal connection .

    External connection is divided into left external connection (LEFT OUTER JOIN or LEFT JOIN)、 Right connection (RIGHT OUTER JOIN or RIGHT JOIN) And all outside connection (FULL OUTER JOIN or FULL JOIN) Three . Unlike the inner connection , The external connection lists not only the rows that match the connection conditions , Instead, list the left table ( Left outer connection )、 Right table ( Right outer connection ) Or two tables ( When fully externally connected ) All data rows that meet the search criteria in the .

       Cross connect (CROSS JOIN) No, WHERE Clause , It returns the Cartesian product of all data rows in the join table , The number of data rows in the result set is equal to the number of qualified data rows in the first table multiplied by the number of qualified data rows in the second table .

       In connection operation ON (join_condition) Clause indicates the join condition , It consists of columns in the connected table and comparison operators 、 Logical operators, etc . No connection can be made to text、ntext and image Data type column for direct connection , But you can connect these three columns indirectly . for example :

SELECT p1.pub_id,p2.pub_id,p1.pr_info FROM pub_info AS p1 INNER JOIN pub_info AS p2

ON DATALENGTH(p1.pr_info)=DATALENGTH(p2.pr_info)
原网站

版权声明
本文为[zhulin1028]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221832211348.html