Search the MySQL manual:
MySQL Manual

Buy this Reference Manual in softcover from Barnes & Noble!

/ Next / Up / Table of Contents

6.4.1 Синтаксис оператора SELECT

Оператор SELECT имеет следующую структуру:

SELECT [STRAIGHT_JOIN]
       [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
       [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] [HIGH_PRIORITY]
       [DISTINCT | DISTINCTROW | ALL]
    select_expression,...
    [INTO {OUTFILE | DUMPFILE} 'file_name' export_options]
    [FROM table_references
      [WHERE where_definition]
      [GROUP BY {unsigned_integer | col_name | formula} [ASC | DESC], ...]
      [HAVING where_definition]
      [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC], ...]
      [LIMIT [offset,] rows]
      [PROCEDURE procedure_name]
      [FOR UPDATE | LOCK IN SHARE MODE]]

SELECT применяется для извлечения строк, выбранных из одной или нескольких таблиц. Выражение select_expression задает столбцы, в которых необходимо проводить выборку. Кроме того, оператор SELECT можно использовать для извлечения строк, вычисленных без ссылки на какую-либо таблицу. Например:

mysql> SELECT 1 + 1;
        -> 2

При указании ключевых слов следует точно соблюдать порядок, указанный выше. Например, выражение HAVING должно располагаться после всех выражений GROUP BY и перед всеми выражениями ORDER BY.

Главы

User Comments

Posted by [name withheld] on Friday July 5 2002, @5:17am [Delete] [Edit]

An example of an inner join on more than two tables
would be useful here. And maybe an explanation of
why the following is important.

If the order is not correct you'll get a syntax error. If
you have a FROM clause with the following pattern:

FROM table1 INNER JOIN (some other join) ON (join
condition).

Change it to:
FROM (some other join) INNER JOIN table1 ON (join
condition).

Posted by on Wednesday December 18 2002, @5:27pm [Delete] [Edit]

The join_condition is used for winnowing the
rows of the "right half" that you want to use for
the cross, whereas the WHERE clause is used for
winnowing the composite.

blah blah

For example, suppose we have a table Men (Man
varchar(15), Lover(15)) and we want to find all
men who had Daisy as a Lover, but not Petunia.
Then we might use:

SELECT M1.Man FROM Men AS M1 LEFT JOIN
Men AS
M2
ON M1.Man=M2.Man AND M2.Lover='Petunia'
WHERE M1.Lover='Daisy' AND M2.Lover IS NULL;

The second part on the ON ensures that we get
NULLs on the right side when Petunia is missing
while the second part of the where picks out
those rows where we actually did get the NULL.

Posted by Phytia King on Thursday August 15 2002, @6:51pm [Delete] [Edit]

i used the LIMIT statement and it prompted
Microsoft OLE DB Provider for ODBC Drivers
(0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax
error in FROM clause
why?

Posted by on Tuesday October 1 2002, @11:47pm [Delete] [Edit]

What is equivalent stmt in MySQL?
select * from table1 where col1 in (select col1 from
table2)

Posted by Jay on Monday October 21 2002, @6:12pm [Delete] [Edit]

How the DISTINCT keyword works:
Rows that are
completely identical *in the end result* are trimmed down to
one.
(Not "rows that are completely identical in all selected tables
are trimmed down to one" or something else)

Posted by Cameron Junge on Wednesday November 13 2002, @4:02pm [Delete] [Edit]

Phytia King: Change the Query Type to pass-
through. (SQL Specific from the Query menu in
Access). LIMIT isn't supported by Access, changing
to pass-through turns off Access's interpretation.

Posted by on Thursday November 21 2002, @1:47am [Delete] [Edit]

MySQL does not support subselects. You can in some
cases get around that with joins (and when you
can, inner joins will give much better performance).

Posted by [name withheld] on Monday January 6 2003, @7:27pm [Delete] [Edit]

I would like to write field names into a outfile
with SELECT command.

SELECT zip,address,name into outfile "/user.txt"
fileds terminated by ',' lines terminated by '\r\n'
from user where name='John'

MySQL 3.23.x is running on my server.

Does it support or not?

J.C. Kim

Posted by on Thursday February 20 2003, @2:44am [Delete] [Edit]

Unfortunately, 'ORDER BY' doesn't seem to enable you to do a natural order (i.e. 1,2,3,4,5,6,7,8,9,10,11,12 etc. instead of 1,10,11,2,3,....)
A possible quick fix is using "ORDER BY LPAD(str,5,"0");"

Instead of the original values, '00010', '00001' and the likes will be sorted, thus giving you a natural order result (original values displayed, since only te values used for the sort operation are LPADded and these are not shown)

Posted by noel darlow darlow on Thursday February 20 2003, @6:51pm [Delete] [Edit]

Re can't sort 1,2, 3, 11, 21, etc: were you sorting on an integer column?

Posted by on Wednesday February 26 2003, @2:10am [Delete] [Edit]

You can simulate a CROSSTAB by the following method:-

Use IF function to select the key value of the sub table as in:

SELECT
SUM(IF(beta_idx=1, beta_value,0)) as beta1_value,
SUM(IF(beta_idx=2, beta_value,0)) as beta2_value,
SUM(IF(beta_idx=3, beta_value,0)) as beta3_value
FROM alpha JOIN beta WHERE alpha_id = beta_alpha_id;

where alpha table has the form alpha_id, alpha_blah, alpha_blah_blah
and beta table has the form beta_alpha_id, beta_other stuff,
beta_idx, beta_value

This will create 3 columns with totals of beta values according to their idx field

Add your own comment.

Top / / Next / Up / Table of Contents