MySql connection Query

Source: Internet
Author: User


MySql connection query provides a summary and a summary of common queries. Prepare data, including two tables, tuser and ttable. The relationship between tuser and ttable is 1-to-many. The tuser table structure is as follows: Name www.2cto.com type Remarks id Integer primary key nameVarchar (50) User Name passwordVarchar (50) User Password SexVarchar (20) User gender ttable table mechanism is as follows: name type Remarks id Integer primary key nameVarchar (50) Name useridInteger user id table creation statement: [SQL] <span style = "font-size: 18px; "> Create table tuser (id Integer primary key, name varchar (50), password varchar (50), sex varchar (20); Create table ttable (id Integer primary key, name varhcar (50), userid Integer); www.2cto.com </span> Insert data statement [SQL] <span style = "font-size: 18px;"> Insert into tuser (id, name, password, sex) values (1, 'Chinese', 'test', 'M'); Insert into tuser (id, name, password, sex) values (2, 'Chinese', 'test', 'M'); Insert into ttable (id, name, userid) values (1, '1', 1); Insert into ttable (id, name, userid) values (2, '2', 2); Insert into ttable (id, name, userid) values (3, '2', 2 ); </span> 1. select * from ttablet left join tuser u on t. userid = 2 and t. userid = u. result:

Based on the left table (ttable), all the Qualified Data is queried, And the right table (tuser) if the condition is not met, null is filled. 2. right Outer join select * from ttable t right join tuser u on t. userid = 2 and t. userid = u. the following result is displayed: www.2cto.com

Based on the right table (tuser), all the data that meets the condition is queried. If the left table (ttable) does not meet the condition, null is filled. 3. The inner joins select * from ttable and tuser; the result is as follows:

Presented in Cartesian product form. Let A and B be A set. Use element x In A as the first element, and element y in B as the second element to form an ordered pair. A set composed of all such ordered pairs, it is called the Cartesian product of A and B and is recorded as A × B. In the database, relationship A is set to <student ID, Name>, specific content: {<1, Zhang San>, <2, Li Si >}; relationship B is set to <student ID, age>, the specific content is {<> }. Then A × B = {<1, Zhang San,>, <1, Zhang San,>, <2, Li Si,>, <2, Li Si, 2, 22 >}if you select the first column = the third column, then only the projection of the first, second, and third columns will be retained, that is, {<1, Zhang San, 20>, <2, Li Si, 22 >}in this way, we found the age of each person through three relational algebra operations. Author Liu Bo

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.