Sortieren nach eingegeben IDs in Where-Klausel
Autor: Ralf v.d.Mark
eingetragen: Dienstag, 29. Juni 2010 um 09:51 Uhr (26/2010 Kalenderwoche)
geändert: Donnerstag, 01. Juli 2010 um 08:22 Uhr (26/2010 Kalenderwoche)
Keywords: order by sortierung
Kategorien: DB: MySQL, DB: MariaDB,
Text:
Wenn man in MySQL in der "WHERE-IN"-Klausel Zahlen eingibt und die Datensätze in der Reihenfolge der Zahlen braucht, kann man folgenden Syntax benutzen:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#funct...
Quellcode:
SELECT feld_id, feld_eins, feld_zwei, feld_usw
FROM tablename
WHERE feld_id IN (1,10,8,5)
ORDER BY FIND_IN_SET(feld_id, '1,10,8,5') ASC
Erklärung:
FIND_IN_SET() -> Return the index position of the first
argument within the second argument
FIND_IN_SET(str, strlist)
Returns a value in the range of 1 to N if the string
str is in the string list strlist consisting of N
substrings. A string list is a string composed of
substrings separated by “,” characters. If the first
argument is a constant string and the second is a
column of type SET, the FIND_IN_SET() function is
optimized to use bit arithmetic. Returns 0 if str is
not in strlist or if strlist is the empty string.
Returns NULL if either argument is NULL. This function
does not work properly if the first argument contains
a comma (“,”) character.