Sziasztok!
Van egy lekérdezésem, ami nem azt a sorrendű eredményt adja vissza amire vágyok:
(SELECT id,prize,upgrades FROM deals WHERE upgrades LIKE '%highlighted%' ORDER BY prize DESC)
UNION
(SELECT id,prize,upgrades FROM deals WHERE upgrades NOT LIKE '%highlighted%' ORDER BY prize DESC)
Az a célom, hogy elöszőr azon sorokat kérdezze le, amelyek upgrades mezőjében szerepel a 'highlighted' szó, ezután csapja hozzá azokat a sorokat, amelyek upgrades mezőjében nem szerepel a 'highlighted' szó. A probléma az, hogy az első SELECT rendesen lekérdezi a sorokat és megfelelően listázza, de a második SELECT nem képes erre. Lekérdezi a 'highlighted'-et nem tartalmazó upgrades mezővel rendelkező sorokat, de azokat nem rendezi prize szerint.
Külön külön a két SELECT rendesen lefut és jól rendezi az eredményt, de együtt nem.
A lekérdezés szempontjából fontos lehet, hogy a lekérdezések még SELECT-enként rendelkeznek JOIN-okkal, de azt állapítottam meg, hogy nem ez okozza a hibát.
Továbbá, az upgrades mező default értéke NULL, és van olyan eset is, amikor üres a mező, ezért a második SELECT WHERE ágában van még néhány kiegészítés, mint: OR upgrades='' OR upgrades IS NULL.
Előre is köszönöm a segítségeteket!
- 4765 megtekintés
Hozzászólások
Magyarazat es megoldas a MySQL manualbol, bepaste-elem ide, jo?
"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 have no effect anyway.
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. The following example uses both clauses:
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
A statement without parentheses is equivalent to one parenthesized as just shown.
This kind of ORDER BY cannot use column references that include a table name (that is, names in tbl_name.col_name format). Instead, provide a column alias in the first SELECT statement and refer to the alias in the ORDER BY. (Alternatively, refer to the column in the ORDER BY using its column position. However, use of column positions is deprecated.)
Also, if a column to be sorted is aliased, the ORDER BY clause must refer to the alias, not the column name. The first of the following statements will work, but the second will fail with an Unknown column 'a' in 'order clause' error:
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY b;
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY a;
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;"
----------------------
"ONE OF THESE DAYS I'M GOING TO CUT YOU INTO LITTLE PIECES!!!$E$%#$#%^*^"
- A hozzászóláshoz be kell jelentkezni
SELECT id,prize,upgrades FROM deals ORDER BY IF(upgrades LIKE '%highlighted%',1,0) DESC,prize DESC
Nem teszteltem, de valami ilyesmi megoldana..
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
--
Yesterday I set my wifi's name to "Hack this if you can".
When I checked it today, it was called "Challenge accepted".
- A hozzászóláshoz be kell jelentkezni
Köszönöm szépen, most már működik minden. Nem értem hogy futhatott át a szemem a manualon, pedig azzal kezdtem ..
-----------------------------------------------------------
Minden lehetséges, csak a lehetetlen tovább tart.
- A hozzászóláshoz be kell jelentkezni
Arra készülj fel, hogy ez a lekérdezés minden lesz csak gyors nem. Könnyen belefuthatsz, nagyobb rekordszámnál abba, hogy a mysql -nek fileban lerakott temptáblában kell rendeznie a találatokat.
----
올드보이
http://molnaristvan.eu/
- A hozzászóláshoz be kell jelentkezni
Ha a "highlighted" egy magic literal, akkor mar beszurasnal kulon oszlopba tegyel flaget (hint: trigger).
A like %akarmi% a letezo leglassabb szelekcio.
- A hozzászóláshoz be kell jelentkezni