I have the following query:

SELECT url, url_hash from pages WHERE url_hash IN (SHA1('URL1'), SHA1('URL2'), SHA1('URL3'))
ORDER BY FIND_IN_SET(url_hash, "SHA1('URL1'), SHA1('URL2'), SHA1('URL3')")

I would to keep the order of the result set the same as the order of the parameters in the IN clause.

I've found this SO question.

The problem is that mysql doesn't know how to interpret a set of SHA1 functions, if I pass them as strings, they are not evaluated correctly, and if I pass them an an expression (without quotes) I get the following error:

OperationalError: (1582, "Incorrect parameter count in the call to native function 'FIND_IN_SET'")
有帮助吗?

解决方案

I think you want to use field() rather than find_in_set():

SELECT url, url_hash
from pages 
WHERE url_hash IN (SHA1('URL1'), SHA1('URL2'), SHA1('URL3'))
ORDER BY field(url_hash, SHA1('URL1'), SHA1('URL2'), SHA1('URL3'));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top