Search the MySQL manual:
MySQL Manual

Buy this Reference Manual in softcover from Barnes & Noble!

/ / Up / Table of Contents

A.5.4 Проблемы с alias

Псевдонимы можно использовать для ссылки на столбец в GROUP BY, ORDER BY или в части HAVING, а также для лучшего именования столбцов:

SELECT SQRT(a*b) as rt FROM table_name GROUP BY rt HAVING rt > 0;
SELECT id,COUNT(*) AS cnt FROM table_name GROUP BY id HAVING cnt > 0;
SELECT id AS "Customer identity" FROM table_name;

Заметим, что в ANSI SQL запрещено ссылаться на псевдоним в определении WHERE. Вызвано это тем, что при выполнении кода WHERE значение столбца может быть еще не определенным. Например, следующий запрос недопустим:

SELECT id,COUNT(*) AS cnt FROM table_name WHERE cnt > 0 GROUP BY id;

Выражение WHERE выполняется, чтобы определить, какие строки следует включить в часть GROUP BY, тогда как HAVING используется для тех строк из результирующего множества, которые должны использоваться.

User Comments

Posted by [name withheld] on Thursday June 6 2002, @4:24am [Delete] [Edit]

Can someone tell me how to work around the sub-select that is not possible in MYSQL 3.23.x just in MYSQL? I know that it is possible with JAVA/PERL/PHP/..-scripts and arrays-variables, but I want to avoid these scriptings!

# EXAMPLE TABLES AND VALUES
drop table if exists more, less;
create table more (id integer(6));
create table less (id integer(6));
insert into more values (1), (2), (3), (4), (5), (6), (7), (8);
insert into less values (1), (2), (3), (4);

# SELECT BEFORE
select id as more from more;
#+------+
#| more |
#+------+
#| 1 |
#| 2 |
#| 3 |
#| 4 |
#| 5 |
#| 6 |
#| 7 |
#| 8 |
#+------+
select id as less from less;
#+------+
#| less |
#+------+
#| 1 |
#| 2 |
#| 3 |
#| 4 |
#+------+

#NOT POSSIBLE WITH MYSQL 3.23.x:
#delete from more where id not in (select id from less);

#WITH HARD-CODED VALUES IT WOULD BE:
delete from more where id not in (1, 2, 3, 4);

# SELECT AFTER
select id as more from more;
#+------+
#| more |
#+------+
#| 1 |
#| 2 |
#| 3 |
#| 4 |
#+------+

select id as less from less;
#+------+
#| less |
#+------+
#| 1 |
#| 2 |
#| 3 |
#| 4 |
#+------+

Add your own comment.

Top / / / Up / Table of Contents