Multi-table Joint query

Source: Internet
Author: User

How to make a three-table join query in SQL
The simplest of

From Table1,table2,table3
where Table1.id=table2.id and Table2.id=table3.id and Table1.id=table3.id
---------------------------------------------------------------------------------------------- How to make a three-table join query in SQL


Left Join


SELECT * from Tab1 left JOIN tab2 on (tab1.size =tab2. ' Size ')




SELECT * from Tab1 left JOIN tab2 on (tab1.size =tab2. ' Size ') WHERE tab2. ' Name ' = ' AAA

'

SELECT * from Tab1 left JOIN tab2 on (tab1.size =tab2. ' Size ') WHERE tab2. ' Name ' = ' AAA

\'



INNER JOIN

SELECT * from Tab1 INNER JOIN tab2 on (tab1.size =tab2. ' Size ')



Like this two SQL results

SELECT * from Tab1 INNER JOIN tab2 on (tab1.size =tab2. ' Size ') WHERE tab2. ' Name ' = ' AAA '


SELECT * from Tab1 INNER JOIN tab2 on (tab1. ' Size ' =tab2. ' Size ' and tab2. ' Name ' = ' AAA ')





Rigt Join


SELECT * from Tab1 INNER JOIN tab2 on (tab1.size =tab2. ' Size ')



SELECT * from Tab1 rightjoin tab2 on (tab1.size =tab2. ' Size ') WHERE tab2. ' Name ' = ' AAA '




SELECT * from Tab1 right JOIN tab2 on (tab1. ' Size ' =tab2. ' Size ' and tab2. ' Name ' = ' AAA ')



Reprint part

http://zac110.iteye.com/blog/1582382


concat function in MySQL (connection string)   Blog Categories: Mysql

concat function in MySQL
How to use:
CONCAT (STR1,STR2,...)

Returns a string resulting from a connection parameter. If any of the arguments are null, the return value is null.

Attention:
If all parameters are non binary strings, the result is a non binary string.
If the argument contains any twos feed string, the result is a binary string.
A numeric parameter is converted to an equal binary string format; To avoid this, you can use explicit type cast, for example:
SELECT CONCAT (CAST (Int_col as CHAR), Char_col)

MySQL's concat function can connect one or more strings, such as
Mysql> Select Concat (' 10 ');
+--------------+
| Concat (' 10 ') |
+--------------+
| 10 |
+--------------+
1 row in Set (0.00 sec)

Mysql> Select concat (' 11 ', ' 22 ', ' 33 ');
+------------------------+
| Concat (' 11 ', ' 22 ', ' 33 ') |
+------------------------+
| 112233 |
+------------------------+
1 row in Set (0.00 sec)

The MySQL concat function returns null if one of the strings is null when the string is concatenated
Mysql> Select Concat (' One ', ' all ', null);
+------------------------+
| Concat (' One ', ', ', null) |
+------------------------+
| NULL |
+------------------------+
1 row in Set (0.00 sec)

concat_ws function in MySQL
How to use:
Concat_ws (SEPARATOR,STR1,STR2,...)

Concat_ws () represents CONCAT with Separator and is a special form of CONCAT (). The first parameter is the separator for the other arguments. The position of the separator is placed between the two strings to which you want to connect. The delimiter can be a string, or it can be another parameter.
Attention:
If the delimiter is null, the result is null. function ignores NULL values after any of the delimiter parameters.

Separated by commas after connection
Mysql> Select Concat_ws (', ', ' 11 ', ' 22 ', ' 33 ');

+-------------------------------+
| Concat_ws (', ', ' 11 ', ' 22 ', ' 33 ') |
+-------------------------------+
| 11,22,33 |
+-------------------------------+
1 row in Set (0.00 sec)

Unlike the concat function in MySQL, the CONCAT_WS function does not return NULL because of null values at execution time.
Mysql> Select Concat_ws (', ', ', ', ', ');
+-------------------------------+
| Concat_ws (', ', ' One ', ' ', ', NULL ') |
+-------------------------------+
| 11,22 |
+-------------------------------+
1 row in Set (0.00 sec)

group_concat function in MySQL
The complete syntax is as follows:
Group_concat ([DISTINCT] The field to connect [order by Asc/desc sort field] [Separator ' delimiter]]

Basic Query

Mysql> select * from AA;
+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
| 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in Set (0.00 sec)

Grouped by ID, print the value of the Name field on one line, separated by commas (default)

Mysql> Select Id,group_concat (name) from AA Group by ID;
+------+--------------------+
| id| Group_concat (name) |
+------+--------------------+
|1 | 10,20,20|
| 20 |
|3 | 200,500|
+------+--------------------+
3 Rows in Set (0.00 sec)

Grouped by ID, print the value of the Name field on one line, separated by semicolons

Mysql> Select ID,GROUP_CONCAT (name separator '; ') from the AA group by ID;
+------+----------------------------------+
| id| Group_concat (name separator '; ') |
+------+----------------------------------+
|1 | 10;20;20 |
| 20|
|3 | 200;500 |
+------+----------------------------------+
3 Rows in Set (0.00 sec)

Grouped by ID, the value of the Name field to be redundant is printed on one line,

Comma separated

Mysql> Select Id,group_concat (distinct name) from the AA group by ID;
+------+-----------------------------+
| id| Group_concat (distinct name) |
+------+-----------------------------+
|1 | 10,20|
| 20 |
|3 | 200,500 |
+------+-----------------------------+
3 Rows in Set (0.00 sec)

Group by ID, print the value of the Name field on one line, comma separated, and then name in reverse order

Mysql> Select Id,group_concat (name order BY name Desc) from the AA group by ID;
+------+---------------------------------------+
| id| Group_concat (name order BY name Desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
| 20|
|3 | 500,200|
+------+---------------------------------------+
3 Rows in Set (0.00 sec)

repeat () function

Used to copy strings, as follows ' AB ' to indicate the string to be copied, 2 for the number of copies copied

Mysql> Select repeat (' AB ', 2);

+----------------+
| Repeat (' AB ', 2) |
+----------------+
| Abab |
+----------------+

1 row in Set (0.00 sec)

And AS
Mysql> Select repeat (' a ', 2);

+---------------+
| Repeat (' a ', 2) |
+---------------+
| AA |
+---------------+
1 row in Set (0.00 sec)
How to make a three-table join query in SQL

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.