Mysql Query records in a table not in another table _ MySQL

Source: Internet
Author: User
Mysql queries records that are not in the other table in one table, bitsCN.com

Mysql queries records that are not in the same table.

Problem:

The field records for querying one table (tb1) are not in the other table (tb2 ).

Condition: The key value of the tb1 field is not in the tbl2 table.

----------------------

Original statement:

Select A. * from tbl1 A where A. key not in (select key from tbl2)

If the data volume in the tbl2 table is large, such as millions of data records, the matching efficiency will be very low each time.

Solution:

SELECT tb1. * FROM tb1 left join tb2 ON tb1.id = tb2.id WHERE tb2.id is null;

Join keywords in SQL statements are commonly used and not easy to understand. the following example

I have provided a simple explanation, and I believe it will inspire you.

-- Create table table1, table2:

Create table table1 (id int, name varchar (10 ))

Create table table2 (id int, score int)

Insert into table1 select 1, 'Lil'

Insert into table1 select 2, 'Zhang'

Insert into table1 select 4, 'Wang'

Insert into table2 select 1, 90

Insert into table2 select 2,100

Insert into table2 select 3, 70

Such as table

-------------------------------------------------

Table1 | table2 |

-------------------------------------------------

Id name | id score |

1 lee | 1 90 |

2 zhang | 2 100 |

4 wang | 3 70 |

-------------------------------------------------

Run the following commands in the query Analyzer:

1. external connection

1. concept: Including left outer join, right outer join or complete external join

2. left join: left join or left outer join

(1) the result set of the left outer join includes all rows in the LEFT table specified in the left outer clause, not just

. If a row in the left table does not match a row in the right table, all the selection list columns in the right table in the row of the associated result set

All are null values ).

(2) SQL statements

Select * from table1 left join table2 on table1.id = table2.id

------------- Result -------------

Id name id score

------------------------------

1 lee 1 90

2 zhang 2 100

4 wang NULL

------------------------------

Note: all the clauses containing Table 1 return the corresponding fields of table 2 based on the specified conditions. The non-conforming fields are displayed as null.

3. right join: right join or right outer join

(1) The right outer join is the reverse join of the left outer join. All rows in the right table are returned. If a row in the right table does not exist in the left table

If the row is matched, a null value is returned for the left table.

(2) SQL statements

Select * from table1 right join table2 on table1.id = table2.id

------------- Result -------------

Id name id score

------------------------------

1 lee 1 90

2 zhang 2 100

NULL 3 70

------------------------------

Note: all the clauses containing Table 2 Return the corresponding fields of Table 1 based on the specified conditions. The non-conforming fields are displayed as null.

4. complete external join: full join or full outer join

(1) the complete external join returns all rows in the left and right tables. When a row does not match a row in another table, the other table

The selected list column contains a null value. If there are matched rows between tables, the entire result set row contains the data value of the base table.

(2) SQL statements

Select * from table1 full join table2 on table1.id = table2.id

------------- Result -------------

Id name id score

------------------------------

1 lee 1 90

2 zhang 2 100

4 wang NULL

NULL 3 70

------------------------------

Note: returns the sum of left and right connections (see upper left and right connections)

2. internal connection

1. concept: inner join is a join that uses a comparison operator to compare the values of the columns to be joined.

2. inner join: join or inner join

3. SQL statements

Select * from table1 join table2 on table1.id = table2.id

------------- Result -------------

Id name id score

------------------------------

1 lee 1 90

2 zhang 2 100

------------------------------

Note: Only the table1 and table2 columns that meet the conditions are returned.

4. equivalent (same as the following execution)

A: select a. *, B. * from table1 a, table2 B where a. id = B. id

B: select * from table1 cross join table2 where table1.id = table2.id (note: cross join

Only the where clause can be used, but not the on clause)

III. Cross-join (complete)

1. concept: a cross join without a WHERE clause will generate the Cartesian product of the table involved in the join. Multiply the number of rows in the first table

The number of rows in the second table is equal to the size of the Cartesian result set. (Table1 and table2 generate 3*3 = 9 Records)

2. cross join: cross join (without the condition where ...)

3. SQL statements

Select * from table1 cross join table2

------------- Result -------------

Id name id score

------------------------------

1 lee 1 90

2 zhang 1 90

4 wang 1 90

1 lee 2 100

2 zhang 2 100

4 wang 2 100

1 lee 3 70

2 zhang 3 70

4 wang 3 70

------------------------------

Note: 3*3 = 9 records are returned, that is, Cartesian product.

4. equivalent (same as the following execution)

A: select * from table1, table2

Left join: If you want to associate three tables, use two left outer join and on combinations.

BitsCN.com

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.