Страница 1 из 1

Видимо баг в MySQL 5.1.42 с сортировкой и UNION

Добавлено: 2010-01-09 17:48:47
goshanecr
Привет всем.
OS: FreeBSD 8.0 i386
MySQL: 5.1.42
/etc/make.conf

Код: Выделить всё

CPUTYPE=athlon64
CFLAGS=-O2 -pipe -msse -msse2 -mmmx -m3dnow -mtune=athlon64 -fno-strict-aliasing
COPTFLAGS=-O2 -pipe -msse -msse2 -mmmx -m3dnow -mtune=athlon64
WANT_MYSQL_VER=51
.if ${.CURDIR} == ${PORTSDIR}/database/mysql51-server
        WITH_CHARSET=utf8
        WITH_XCHARSET=all
        WITH_COLLATION=utf8_general_ci
        WITH_OPENSSL=yes
        WITH_FAST_MUTEXES=yes
        BUILD_OPTIMIZED=yes
.endif
так вот есть таблица

Код: Выделить всё

myTable (id int unsigned primary key auto_increment, orderID int unsigned)
в случае запроса

Код: Выделить всё

SELECT id,0 FROM myTable ORDER BY orderID ASC
всё нормально, а вот в случае:

Код: Выделить всё

(SELECT 9999,COUNT(id) FROM myTable) 
UNION 
(SELECT id,0 FROM myTable ORDER BY orderID ASC)
выбирает второй SELECT без сортировки... Раньше вроде было всё норм.. не заметил в какой момент это произошло, но вот сейчас так. Это баг MySQL или это я чего-то не понимаю?

Re: Видимо баг в MySQL 5.1.42 с сортировкой и UNION

Добавлено: 2010-01-09 18:27:27
zg
это не баг, так и задумано
http://dev.mysql.com/doc/refman/5.1/en/union.html

Код: Выделить всё

(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
However, use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will o determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because ithave no effect anyway.

Re: Видимо баг в MySQL 5.1.42 с сортировкой и UNION

Добавлено: 2010-01-10 21:50:50
goshanecr
Точно... заработало :) Спасибо что ткнул носом :)