Eit*_*tan 1 sql parameters matching
我正在编写sql来通过许多参数搜索数据库.我如何按照与where子句中的大多数参数匹配的项来排序结果集.例如:
SELECT *
FROM users
WHERE username = 'eitan'
OR email = 'eitan@eitan.com'
OR company = 'eitan'
Username | email | company
1) eitan | blah@blah.com | blah
2) eitan | eitan@eitan.com | eitan
3) eitan | eitan@eitan.com | blah
Run Code Online (Sandbox Code Playgroud)
应订购如下:
2,3,1.
谢谢.(ps查询不是那么容易,在WHERE中有很多连接和很多OR)
埃坦
如果MySQL:
SELECT * FROM users
ORDER BY
(username = 'eitan')
+ (email = 'eitan@eitan.com')
+ (company = 'eitan')
DESC
Run Code Online (Sandbox Code Playgroud)
如果PostgreSQL:
SELECT * FROM users
ORDER BY
(username = 'eitan')::int
+ (email = 'eitan@eitan.com')::int
+ (company = 'eitan')::int
DESC
Run Code Online (Sandbox Code Playgroud)
如果是Sql Server:
SELECT * FROM users
ORDER BY
case when username = 'eitan' then 1 else 0 end
+ case when email = 'eitan@eitan.com' then 1 else 0 end
+ case when company = 'eitan' then 1 else 0 end
DESC
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
77 次 |
最近记录: |