MySQL learning footprint record 13 -- JOIN table -- inner join... ON

Source: Internet
Author: User
Tags database join

MySQL learning footprint record 13 -- JOIN table -- inner join... ON learning database join table this section content almost vomit blood, read ON the introduction and Baidu to the content, always feel a blank face ..... maybe I have a poor understanding. But I am a person who won't give up easily. After studying for half an hour, I finally figured out the connection principle and summarized myself as follows: The key lies in "select statement. If there is no WHERE clause limit, returns the Cartesian product of two tables."

Example: Table 1: Kemu + ----- + -------- + | kno | kname | + ----- + -------- + | B | yuwen | b1 | shuxue | + ----- + -------- + Table 2: chenji; + ------ + --------- + | sno | kno | chengji | + ------ + --------- + | 1 | a | 10 | 2 | a1 | 20 | + ------ + --------- + 2 rows in set (0.00 sec) * Key: select statement. If no WHERE clause is specified, the Cartesian product of the two tables is returned. mysql> SELECT * FROM Kemu, Chenji; + ----- + -------- + ------ + --------- + | kno | kname | sno | kno | chengji | + ----- + -------- + ------ + --------- + | B | yuwen | 1 | a | 10 | b1 | shuxue | 1 | a | 10 | B | yuwen | 2 | a1 | 20 | b1 | shuxue | 2 | a1 | 20 | + ----- + -------- + ------ + --------- + 4 rows in set (0.00 sec)

 

Tips: all the tables after the SELECT statement are merged into a large table. Therefore, the clauses following the SELECT statement are equivalent to filtering data in a table. 1. foreign key: a column of a table that contains the primary key value of another table and defines the relationship between two tables. the join is not a physical entity. It does not exist in the actual database table. It exists in the execution of the query. 3. create a connection
Example: mysql> SELECT vend_name, prod_name, prod_price-> FROM vendors, products-> WHERE vendors. vend_id = products. vend_id # The WHERE clause indicates that MySQL matches the vendors table-> order by vend_name, prod_name; # vend_id and vend_id in the products table. + ------------- + region + ------------ + | vend_name | prod_name | prod_price | + ------------- + region + ------------ + | ACME | Bird seed | 10.00 | ACME | Carrots | 2.50 | ACME | Detonator | 13.00 | ACME | Safe | 50.00 | ACME | Sling | 4.49 | ACME | TNT (1 stick) | 2.50 | ACME | TNT (5 sticks) | 10.00 | Anvils R Us |. 5 ton andevil | 5.99 | Anvils R Us | 1 ton andevil | 9.99 | Anvils R Us | 2 ton andevil | 14.99 | Jet Set | JetPack 1000 | 35.00 | Jet Set | JetPack 2000 | 55.00 | LT Supplies | Fuses | 3.42 | LT Supplies | Oil can | 8.99 | + --------------- + -------------- + ---------- + 14 rows in set (0.01 sec)

 

TIPS: * When the WHERE clause is used to join two tables, each row in the first table is paired with each row in the second table. and filter the rows that do not meet the conditions. 4. inner join .... ON
Example: mysql> SELECT vend_name, prod_name, prod_price-> FROM vendors inner join products # use a clear JOIN syntax to ensure that you do not forget the JOIN conditions-> ON vendors. vend_id = products. vend_id # The join condition uses a specific ON clause,-> order by vend_name, prod_name; # The actual conditions passed are the same as those passed to WHERE + ------------- + ---------------- + ---------- + | vend_name | prod_name | prod_price | + ------------- + region + ------------ + | ACME | Bird seed | 10.00 | | ACME | Carrots | 2.50 | ACME | Detonator | 13.00 | ACME | Safe | 50.00 | ACME | Sling | 4.49 | ACME | TNT (1 stick) | 2.50 | ACME | TNT (5 sticks) | 10.00 | Anvils R Us |. 5 ton andevil | 5.99 | Anvils R Us | 1 ton andevil | 9.99 | Anvils R Us | 2 ton andevil | 14.99 | Jet Set | JetPack 1000 | 35.00 | Jet Set | JetPack 2000 | 55.00 | LT Supplies | Fuses | 3.42 | LT Supplies | Oil can | 8.99 | + --------------- + -------------- + ---------- + 14 rows in set (0.00 sec)

 

5. Join multiple tables * SQL has no limit on the number of tables that can be joined in a SELECT statement.
   Example:    mysql> SELECT prod_name,vend_name,prod_price,quantity            -> FROM orderitems,products,vendors            -> WHERE products.vend_id=vendors.vend_id           -> AND orderitems.prod_id=products.prod_id           -> AND order_num=20005;+----------------+-------------+------------+----------+| prod_name      | vend_name   | prod_price | quantity |+----------------+-------------+------------+----------+| .5 ton anvil   | Anvils R Us |       5.99 |       10 || 1 ton anvil    | Anvils R Us |       9.99 |        3 || TNT (5 sticks) | ACME        |      10.00 |        5 || Bird seed      | ACME        |      10.00 |        1 |+----------------+-------------+------------+----------+4 rows in set (0.00 sec)

 


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.