Basic Database learning-how to connect SQL statements

Source: Internet
Author: User
Database basic learning-the connection method of SQL statements encountered in the process of knocking on the data center charging system, that is, the data I need is in two tables, how can I query two tables at the same time? After searching and asking, I got a solution. I think it is also a simple solution, that is to set

Database basic learning-the connection method of SQL statements encountered in the process of knocking on the data center charging system, that is, the data I need is in two tables, how can I query two tables at the same time? After searching and asking, I got a solution. I think it is also a simple solution, that is to set


Basic Database learning-how to connect SQL statements

When I knocked on the data center charging system, I encountered such a problem that the data I needed was in two tables, how can I query two tables at the same time? The problem was solved by searching and asking the students?

Solution 1: it is also a simple solution I want to solve, that is, to extract the data in Table a that needs to be called by a table and assign it to my own Defined variables, then open another table B and assign values to the corresponding fields. This is simple, but I don't think it is a fundamental solution. So we found the following method. Www.2cto.com

Solution 2: This involves the connection of SQL statements. The connection of SQL statements is divided into the following types:

External Connection

A. left outer join:

Left Outer Join (left join): the result set contains the matched rows in the connected table, and all rows in the left connected table.

SQL: select a. a, a. B, a. c, B. c, B. d, B. f from a left out join B ON a. a = B. c

B: right outer join: www.2cto.com

Right Outer Join (right join): the result set includes both matched join rows in the connection table and all rows in the right join table.

C: full outer join:

Full outer join: includes not only matching rows in the symbolic join table, but also all records in the two join tables.

Internal Connection

The connection query operation in the internal connection lists the data rows that match the connection condition. It compares the column values of the connected columns using the comparison operator.

Www.2cto.com

Intranet connections are divided into three types:

1. equijoin: Use the equal sign (=) operator in the connection condition to compare the column values of connected columns. All columns in the connected table, including duplicate columns, are listed in the query results.

Use equijoin to list duplicate columns (TownCode) in the LandTbl and TownTbl tables in the selection list ):

Example: Select * fromLandTbl as A inner join TownTbl as B on A. TownCode = B. TownCode

2. Unequal join: Use a comparison operator other than the equal operator to compare the column values of the connected columns.

These operators include >,>=, <=, <,!> ,! <和<> .

Example: Select * fromLandTbl as A inner join TownTbl as B on A. TownCode <> B. TownCode

3. Natural join: Use the equal to (=) operator in the connection condition to compare the column values in the connected column. However, it uses the selection list to indicate the columns included in the query result set, delete duplicate columns in the connection table.

Delete duplicate columns (TownCode) in the LandTbl and TownTbl tables in the selection list using a natural connection ):

Www.2cto.com

Select A. *, B. CityCode, B. TownName from LandTbl as A inner join TownTbl as B onA. TownCode = B. TownCode

An internal join is also called an equivalent join. The returned result set is all the matched data in the two tables, and the unmatched data is discarded. That is to say, in this query, DBMS only returns relevant rows from the source table, that is, the query result table contains two source table rows, which must meet the search conditions in the ON clause. As a comparison, if the rows in the source table do not have corresponding (related) rows in the other table, the row will be filtered out and will not be included in the result table. Internal connections are completed using comparison operators.

I used an internal connection during the query process, so that I can query the content of both tables at the same time.

Select * from line_Info inner join student_Info on line_info.cardno = student_Info.cardno where line_Info.cardno = '"& txtCardNo. Text &"'"

Www.2cto.com

In addition, when learning this, I also learned how to INSERT the content of a table into another table in the database statements. This involves the use of INSERT.

Before inserting data, make sure that the fields in the two tables are consistent.

Insert into [destination table] (Field 1, Field 2, Field 3 ......) Select Field 1, Field 2, Field 3 ......) From [source table] where conditions for filtering data. You can.

It seems that there are too many SQL statements and databases to learn! I only touched a little bit.

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.