SCC
SCC

Reputation: 509

MySQL SELECT 1 AS sort_col: is there is a column for 1?

From Mysql Documentation :

To cause rows in a UNION result to consist of the sets of rows retrieved by each SELECT one after the other, select an additional column in each SELECT to use as a sort column and add an ORDER BY following the last SELECT:

(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;

To additionally maintain sort order within individual SELECT results, add a secondary column to the ORDER BY clause:

(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col, col1a;

Use of an additional column also enables you to determine which SELECT each row comes from. Extra columns can provide other identifying information as well, such as a string that indicates a table name.

My Question:

Do I need to create two additional columns in the table for this purpose?

Upvotes: 0

Views: 828

Answers (1)

Sebas
Sebas

Reputation: 21522

No. The sort_col of the mysql manual is created on the fly with constant values (1, and 2).

Analogically, you can SELECT any value you wish with no need of any table:

SELECT 1, 2, 3 FROM dual;

Upvotes: 1

Related Questions