sql使用逻辑控制语句

现实生活中有许多使用枚举的状态码,在一些查询SQL中其实是可以把这些枚举对应出来的。
例如支付状态status 0, 1,2,3,4
case
when ‘条件1’ then ‘值1’
when ‘条件2’ then ‘值2’
else ‘值3’
end

例子:

SELECT  id,  case 
when status =2 then '待使用'
when status =3 then '已使用'
when status = 4 then '使用中'
end	 as `使用状态` FROM test_table ORDER BY `id` DESC  LIMIT 0,50;

mysql 索引

https://tech.meituan.com/2014/06/30/mysql-index.html

尽量选择区分度高的列作为索引,区分度的公式是count(distinct col)/count(*),表示字段不重复的比例,比例越大我们扫描的记录数越少,唯一键的区分度是1,而一些状态、性别字段可能在大数据面前区分度就是0,那可能有人会问,这个比例有什么经验值吗?使用场景不同,这个值也很难确定,一般需要join的字段我们都要求是0.1以上,即平均1条扫描10条记录。

发布了370 篇原创文章 · 获赞 81 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/lineuman/article/details/102703523