mysql connection in union

SELECT ...
UNION [ALL | DISTINCT]
SELECT ...
[UNION [ALL | DISTINCT]
SELECT ...]

UNION for the result from a number of SELECT statements into a single result set.

Listed in the corresponding positions of each SELECT statement is selected columns should have the same type. (E.g., first sentence of the selected first column should have the same type and the first column is selected, other sentences.) Column names are used in the first SELECT statement is also used for the column name of the result.

The SELECT statement is a conventional selection statement, but is defined by the following:

· Only the last SELECT statement can use INTO OUTFILE.

· HIGH_PRIORITY can not be used as part of the UNION SELECT statement. If you specify HIGH_PRIORITY for the first SELECT, it will not work. If you specify HIGH_PRIORITY other subsequent SELECT statements, a syntax error will be generated.

If you do not use the keyword ALL for the UNION, all returned rows are unique, like you have the whole set of results using DISTINCT. If you specify ALL, you'll get all matching rows from all the used SELECT statements.

DISTINCT keyword is an optional word, can not afford to have any effect, but according to the requirements of the SQL standard, the syntax allows the use. (In MySQL, DISTINCT moving body on behalf of a default common properties.)

You can mix UNION ALL and UNION DISTINCT in the same query. UNION types to be mixed in such a manner as to treat, i.e. to cover all ALL DISTICT union common body is located on its left. DISTINCT union can be used specifically to generate the UNION DISTINCT, or UNION (not followed by a keyword DISTINCT or ALL) implicitly generated.

If you want to use ORDER BY or LIMIT clause to sort or limit the entire UNION result, it should have a single SELECT statement to add parentheses, and put the ORDER BY or LIMIT after the last one. The following example uses both of these two clauses:

(SELECT a FROM tbl_name WHERE a=10 AND B=1)
UNION
(SELECT a FROM tbl_name WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;

- taken to ensure consistent field 
( SELECT s.sc_id ID, name s.score from SC S) Union ( SELECT t.c_id ID, name t.`name` from Course T) the ORDER BY ID limit 10 ;

 

This table includes ORDER BY name can not be used (i.e., using tbl_name . Col_name name format) column references. It can be provided in the first SELECT statement in a column alias, and refer to the alias in the ORDER BY or use the column position in the ORDER BY column see in. (Preferred use of aliases, because they do not recommend the use of column positions.)

In addition, if there is a classification with an alias, the ORDER BY clause must refer to the alias, not the column name references. The following statement is the first statement must be running, but the second will fail, appear in 'order clause' in Unknown column 'a' errors:

(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY b;
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY a;

To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT: In order to use a single SELECT ORDER BY or LIMIT, should be placed in parentheses clause. Parentheses contain SELECT:

(SELECT a FROM tbl_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10);

ORDER BY parentheses for a single SELECT statement only when combined with LIMIT, to function. Otherwise, ORDER BY is optimized removed.

UNION type and length of the column in the result set value is restored considering all SELECT statements. For example, consider the following statement:

mysql> SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
+---------------+
| REPEAT('a',1) |
+---------------+
| a             |
| bbbbbbbbbb    |
+---------------+

(In earlier versions of MySQL portion, the second line has been truncated to a length of 1.)

Guess you like

Origin www.cnblogs.com/fdy-study-consist/p/12113563.html