Possible duplicates:Order MySQL results by IN sequence?Ordering in order of value in an SQL IN () clause
I have the following table called "Products"
id desc 1 BL10 2 BL15 3 BL45 4 BL50
Well, this is my request. SELECT * FROM Products WHERE id IN(3,1,4,2) I want it to appear in the same order as the IN clause, i.e. 3,1,4,2:
SELECT * FROM Products WHERE id IN(3,1,4,2)
id desc 3 BL45 1 BL10 4 BL50 2 BL15
but when I execute it, it shows the ordered, How can I get it? I am using MSSQL 2005
Your proposal INwill not sort your result set, but instead, your results will be returned in the order in which they appear in the request.
IN
, ORDER BY , :
ORDER BY
SELECT * FROM Products WHERE id IN (3,1,4,2) ORDER BY (CASE WHEN id = 3 THEN 0 WHEN id = 1 THEN 1 WHEN id = 4 THEN 2 WHEN id = 2 THEN 3 END)
( DBMS-, , , , )
SELECT * FROM Products WHERE id IN (3, 1, 4, 2) ORDER BY FIELD (id, 3, 1, 4, 2)
, , , , SortOrder, ORDER BY:
SELECT * FROM WHERE id IN (3,1,4,2) ORDER BY SortOrder
SortOrder :
Id SortOrder 1 20 2 40 3 10 4 30
Source: https://habr.com/ru/post/1761590/More articles:Call the AOP proxy by calling a method in bean - javaКак правильно лицензировать виджет в отличие от приложения? - androidWhat is the difference between a semaphore and a critical region? - semaphoreUNIX BASH: extract a number from a string - stringhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1761589/in-a-sitemap-is-it-advisable-to-include-links-to-every-page-on-the-site-or-only-ones-that-need-it&usg=ALkJrhi_nMiAZrUhkqC31UG4q5WlNeExDwHow to override TimePattern for 12 hours? - .netJQuery selector help - jqueryReading a domain cookie from a subdomain with Rails - ruby-on-railsHow to create a top layer that ignores mouse clicks, etc. - cocoaМакрос для принудительного вывода Excel из режима редактирования - excel-vbaAll Articles