MySQL Table connection

Source: Internet
Author: User

When you need to display fields from multiple tables at the same time, you can use table joins to implement such a feature.
  • From the big class above, the table joins into the inner and outer joins, the most important difference between them is that the internal connection only selects the records that match each other in two tables, and the outer joins will select other mismatched records. What we use most is the inner connection.
    • For example: query out the name of all employees and the department name, because employee names and departments are stored in the table EMP and dept, so you need to use a table connection to query:
      • Select Ename,deptname from emp,dept where Emp.deptno=dept.deptno; Ename is the field in the table EMP Deptname is the field condition in the table dept indicates that the table EMP and the table dept deptno the same record concatenated into a record, from which the ename and Deptname fields are detected
  • The outer joins are divided into left and right connections, which are defined as follows: Left JOIN: Contains all the records in the left table and even the right link in the right table that does not match it: contains all the records in the right table and even the records that do not match it in the left table
    • Left join: Select Ename,deptname from the EMP left JOIN dept on Emp.deptno=dept.deptno, right connection: Select Ename,deptname from dept R join EMP on Dept.deptno=emp.deptno;
    • These two look exactly the same. records that contain all of the records in the EMP table, even those that don't match it in the Dept table.
  • In some cases, when we query, the condition is the result of the other SELECT statement, this time, we need to use the subquery
  • The keywords used for subqueries mainly include in, not in, =,! =, exists, not exists, and so on.
  • SELECT * from EMP where deptno in (select Deptno from dept);
    If the subquery record number is unique, you can also use = instead of In:
    SELECT * from emp where deptno = (select Deptno from dept); ERROR 1242 (21000): subquery returns more than 1 row
    SELECT * from emp where deptno = (select Deptno from dept limit 1); Ok
  • In some cases, subqueries can be converted to table joins, for example: SELECT * from EMP where deptno in (select Deptno from dept); Select emp.* from EMP, dept where Emp.deptno=dept.deptno;

Note: The conversion between subqueries and table joins is primarily applied in two ways: MySQL 4.1 does not support subqueries, and table joins are used to implement the function table connection of subqueries in many cases to optimize subqueries

  • We often encounter such an application, the two table data according to a certain query criteria, the results are merged together to display, this time, it is necessary to use the Union and the Union ALL keyword to implement such a function, the specific syntax is as follows: SELECT * from T1

  • union| UNION All

  • SELECT * from T2 ...

  • union| UNION All

  • SELECT * from TN;

  • The main difference between union and union all is that union all merges the result set directly, and the Union is distinct the result of the Union all once, removing the result of the duplicate record.

    • For example, the collection of department numbers in the EMP and dept tables is displayed:

    • < EM id= "__mcedel" > select deptno from EMP

    • < EM id= "__mcedel" > -> UNION ALL

    • Select Deptno from dept;

MySQL Table connection

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.