Home  >  Article  >  Database  >  How to connect multiple tables in MySQL using sql statements

How to connect multiple tables in MySQL using sql statements

(*-*)浩
(*-*)浩Original
2019-05-07 10:22:414646browse

There are many ways to write multi-table SQL statements in MySql. This article will explain and use multi-table query statements.

Recommended course: MySQL tutorial

How to connect multiple tables in MySQL using sql statements

##In SQL language, there are two ways Specify an alias for the table
The first is to specify it through the keyword AS,
The second is to add the alias of the table directly after the table name
You should pay attention to a few points when using the alias of the table
(1) An alias is usually a shortened table name, used to refer to a specific column in a table in a connection. If there are columns with the same name in multiple tables in the connection, the table name or table name must be used. Alias ​​qualified column name
(2) If the alias of the table is defined, the table name cannot be used

1. Use the SELECT clause for multi-table query

SELECT field name FROM Table 1, Table 2... WHERE Table 1. Field = Table 2. Field AND other query conditions

Example:

SELECT a.id,a.name,a.address,a.date,b.math,b.english,b.chinese FROM tb_1 AS b,tb_2 AS a WHERE a.id=b.id
Note: In the above code, two The same id field information in the two tables is used as a condition to establish an association between the two tables, but it should not be used in actual development. It is best to use primary and foreign key constraints to achieve

2. Use table aliases to perform multiple Table query

Example:

SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_1  a,tb_2  b WHERE a.id=b.id AND b.id='$_POST[textid]'
MySQL is a relational database management system developed by the Swedish MySQL AB company and is currently a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is the best RDBMS (Relational Database Management System) application software.

MySQL is a relational database management system. A relational database stores data in different tables instead of putting all data in one large warehouse, which increases speed and flexibility.

The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts a dual licensing policy and is divided into community version and commercial version. Due to its small size, fast speed, low total cost of ownership, and especially the characteristics of open source, MySQL is generally chosen as the website database for the development of small and medium-sized websites.

The above is the detailed content of How to connect multiple tables in MySQL using sql statements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn