您的位置:首页 > 数据库 > MySQL

mysql使用in查询,按照期望的顺序输出结果

2015-12-31 11:42 846 查看
mysql使用in查询,按照期望的顺序输出结果,可以使用field函数

SELECT
ProvinceId,
ProvinceName
FROM
btoc_province
WHERE
ProvinceName IN ('河南', '辽宁', '山西')
ORDER BY
FIELD(
provincename,
'河南',
'辽宁',
'山西'
);


结果:




MySQL FIELD() function

MySQL FIELD() returns the index position of the searching string from a list of strings. If search string is not found, it returns a 0(zero). If search string is NULL, the return value is 0 because
NULL fails equality comparison with any value.

FIELD() is the complement of ELT(). When all arguments of the FIELD() are strings, they are compared as strings. If all arguments are number, they compared as numbers. Other wise all are compared as double.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql field in