Article Title multi-Table query: merged result set, connection query, subquery, and article title merge

Source: Internet
Author: User

Article Title multi-Table query: merged result set, connection query, subquery, and article title merge

Multi-Table query
Category:
* Merge result sets
* Connection Query
* Subquery

[1] merge result sets
* The number of columns in the table to be merged must be the same. The column types must be the same. * union all must be removed for duplicate rows. Duplicate row examples are not removed: select * from table1 union select * from table2; select * from table1 union all select * from table2;
[2] connection Query
CATEGORY * inner connection * outer connection> left outer connection> right outer connection> all outer connections (not supported by MySQL) * natural connections (a simplified method) 1. inner join * Dialect: SELECT * FROM table 1 alias 1, Table 2 alias 2 WHERE alias 1.xx= alias 2.xx( MySQL can) * standard: SELECT * FROM table 1 alias 1 inner join Table 2 alias 2 ON Alias 1.xx= alias 2.xx * nature: SELECT * FROM table 1 alias 1 natural join Table 2 alias 2 is automatically matched using the same columns of the two tables, the same column only shows one column * All records connected to the query in the same column meet the conditions. Note: The condition after where is called the join condition. Cartesian product must be removed for multi-table queries, and association conditions must be removed. outer join * outer left: SELECT * FROM table 1 alias 1 left outer join Table 2 alias 2 ON Alias 1.xx= alias 2.xx// all records of coordinates are displayed, if no corresponding right table exists, the right table is supplemented with null * left outer natural: SELECT * FROM table 1 alias 1 natural left outer join Table 2 alias 2; * Right OUTER: SELECT * FROM table 1 alias 1 right outer join Table 2 alias 2 ON Alias 1.xx= alias 2.xx * right outer nature: SELECT * FROM table 1 alias 1 natural right outer join Table 2 alias 2; * Full link: UNION can be used to complete the full link: left outer union right outer Note: outer link: if the condition is not met, it will also be queried. Left outer right outer uses null complement bit, right outer left uses null complement bit
Subquery
1. location: * The where clause exists as a condition * The from clause exists as a table (generally multiple rows and multiple columns). 2. condition ***** single row Single Column: SELECT * FROM table 1 alias 1 WHERE column 1 [=,>, <,> =, <= ,! =] (SELECT column FROM table 2 alias 2 WHERE condition) *** multiple rows Single Column: SELECT * FROM table 1 alias 1 WHERE column 1 [IN, ALL, ANY] (SELECT column FROM table 2 alias 2 WHERE condition) SELECT * FROM table name WHERE column> ANY (SELECT ....) WHERE condition ** single row and multiple columns: SELECT * FROM table 1 alias 1 WHERE (column 1, column 2) IN (SELECT column 1, column 2 FROM table 2 alias 2 WHERE condition) // The columns corresponding to the where and sub-tables are the same. *** multiple rows and multiple columns: SELECT * FROM table 1 alias 1, (SELECT ....) alias 2 WHERE Condition

Note: For multi-table queries, you need to find association conditions. on is followed by association conditions.

Multi-Table query small exercise

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.