SQL table Connection Query

Source: Internet
Author: User
Tags ming

Two tables:

1Mysql> Select *  fromstudent;2 +----+------+------+3 |Id|Name|Age|4 +----+------+------+5 |  1 |Xiao Wang|    - |6 |  2 |Little Red|    - |7 |  3 |Xiao ming|    - |8 |  4 |Xiao Li|    A |9 |  5 |Jack bauer|    - |Ten +----+------+------+ One 5Rowsinch Set(0.00Sec
1Mysql> Select *  fromMark;2 +----+------+-----------+3 |Id|Mark|StudentID|4 +----+------+-----------+5 |  1 |    A |         2 |6 |  2 |    $ |         2 |7 |  3 |    - |         3 |8 +----+------+-----------+9 3Rowsinch Set(0.00Sec

One, inner connection (inner join)

1Mysql> SelectS.name,m.mark fromStudent S,mark mwhereS.id=M.studentid;2 +------+------+3 |Name|Mark|4 +------+------+5 |Little Red|    A |6 |Little Red|    $ |7 |Xiao ming|    - |8 +------+------+9 3Rowsinch Set(0.07sec)TenMysql> SelectS.name,m.mark fromStudent SInner JoinMark M onS.id=M.studentid; One +------+------+ A |Name|Mark| - +------+------+ - |Little Red|    A | the |Little Red|    $ | - |Xiao ming|    - | - +------+------+ - 3Rowsinch Set(0.06Sec

Second, left join (left join)

1Mysql> SelectS.name,m.mark fromStudent S Left JoinMark M onS.id=M.studentid;2 +------+------+3 |Name|Mark|4 +------+------+5 |Little Red|    A |6 |Little Red|    $ |7 |Xiao ming|    - |8 |Xiao Wang| NULL |9 |Xiao Li| NULL |Ten |Jack bauer| NULL | One +------+------+ A 6Rowsinch Set(0.07Sec

Third, right Join

1Mysql> SelectS.name,m.mark fromStudent S Right JoinMark M onS.id=M.studentid;2 +------+------+3 |Name|Mark|4 +------+------+5 |Little Red|    A |6 |Little Red|    $ |7 |Xiao ming|    - |8 +------+------+9 3Rowsinch Set(0.00Sec

SQL table Connection Query

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.