问题:Mysql将枚举转换为Int [英] ISSUE: Mysql converting Enum to Int

查看:255
本文介绍了问题:Mysql将枚举转换为Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个非常简单的评分系统,其中每个评级被存储为枚举('1',' - 1')。要计算总计我尝试使用这个语句:

  SELECT SUM(CONVERT(rating,SIGNED))作为值从表WHERE _id = 1 

这对正面1可以正常工作,但由于某种原因,-1被解析为2 。



任何人都可以帮助或提供煽动?



或者我应该放弃,只需将列更改为SIGNED INT(1)?

解决方案

是的,我建议更改列的类型。当您阅读有关枚举类型的文档时,问题变得清楚(强烈建议不要使用数字作为枚举值!) - 返回枚举项的索引,而不是枚举值本身。


I have a very simple rating system in my database where each rating is stored as an enum('1','-1'). To calculate the total I tried using this statement:

SELECT SUM(CONVERT(rating, SIGNED)) as value from table WHERE _id = 1

This works fine for the positive 1 but for some reason the -1 are parsed out to 2's.

Can anyone help or offer incite?

Or should I give up and just change the column to a SIGNED INT(1)?

解决方案

Yes, I'd suggest to change the type of the column. The issue becomes clear when you read the doc about enum type (which strongly recommends not to use numbers as enumeration values!) - the index of the enum item is returned, not the enum value itself.

这篇关于问题:Mysql将枚举转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆