SELECT
Syntax
1084
tbl_name
[[AS]
alias
] [
index_hint
]
The use of index hints provides the optimizer with information about how to choose indexes during
query processing. For a description of the syntax for specifying these hints, see
Section 13.2.8.3,
“Index Hint Syntax”
.
You can use
SET max_seeks_for_key=value
as an alternative way to force MySQL to prefer
key scans instead of table scans. See
Section 5.1.4, “Server System Variables”
.
• You can refer to a table within the default database as
tbl_name
, or as
db_name
.
tbl_name
to
specify a database explicitly. You can refer to a column as
col_name
,
tbl_name
.
col_name
, or
db_name
.
tbl_name
.
col_name
. You need not specify a
tbl_name
or
db_name
.
tbl_name
prefix
for a column reference unless the reference would be ambiguous. See
Section 9.2.1, “Identifier
Qualifiers”
, for examples of ambiguity that require the more explicit column reference forms.
• A table reference can be aliased using
tbl_name AS alias_name
or
tbl_name alias_name
:
SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
WHERE t1.name = t2.name;
SELECT t1.name, t2.salary FROM employee t1, info t2
WHERE t1.name = t2.name;
• Columns selected for output can be referred to in
ORDER BY
and
GROUP BY
clauses using column
names, column aliases, or column positions. Column positions are integers and begin with 1:
SELECT college, region, seed FROM tournament
ORDER BY region, seed;
SELECT college, region AS r, seed AS s FROM tournament
ORDER BY r, s;
SELECT college, region, seed FROM tournament
ORDER BY 2, 3;
To sort in reverse order, add the
DESC
(descending) keyword to the name of the column in the
ORDER BY
clause that you are sorting by. The default is ascending order; this can be specified
explicitly using the
ASC
keyword.
If
ORDER BY
occurs within a subquery and also is applied in the outer query, the outermost
ORDER
BY
takes precedence. For example, results for the following statement are sorted in descending
order, not ascending order:
(SELECT ... ORDER BY a) ORDER BY a DESC;
Use of column positions is deprecated because the syntax has been removed from the SQL
standard.
• If you use
GROUP BY
, output rows are sorted according to the
GROUP BY
columns as if you had an
ORDER BY
for the same columns. To avoid the overhead of sorting that
GROUP BY
produces, add
ORDER BY NULL
:
SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL;
• MySQL extends the
GROUP BY
clause so that you can also specify
ASC
and
DESC
after columns
named in the clause:
SELECT a, COUNT(b) FROM test_table GROUP BY a DESC;
• MySQL extends the use of
GROUP BY
to permit selecting fields that are not mentioned in the
GROUP BY
clause. If you are not getting the results that you expect from your query, please read the
description of
GROUP BY
found in
Section 12.15, “Functions and Modifiers for Use with
GROUP BY
Clauses”
.
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...