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:

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

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.

这适用于正1但由于某种原因-1被解析为2。

Can anyone help or offer incite?

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

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

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

5 个解决方案

#1


3

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.

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

更多相关文章

  1. Java MySQL数据类型对照
  2. MySQL数据类型中DECIMAL的作用和用法
  3. 可以在SELECT语句中嵌入描述语法吗?
  4. 如何从MySQL DBs的不同表中提取create语句?
  5. mysql查询一条工单时间需要10秒。优化sql语句得以解决。
  6. MySQL数据库储存bit类型的值报错
  7. MySql SELECT 语句执行顺序
  8. MySQL 数据(字段)类型
  9. MySQL 绿色版基本设置语句

随机推荐

  1. 注册中心 Eureka 源码解析 —— 应用实例
  2. 网关 Spring-Cloud-Gateway 源码解析 —
  3. Spring Cloud Eureka Server高可用之:在线
  4. 注册中心 Eureka 源码解析 —— 应用实例
  5. 注册中心 Eureka 源码解析 —— 应用实例
  6. 一文上手 Elasticsearch常用可视化管理工
  7. Kafka底层原理剖析(近万字建议收藏)
  8. 自然语言处理工具包 HanLP在 Spring Boot
  9. 注册中心 Eureka源码解析 —— 应用实例
  10. 老艿艿说:关于时间管理的分享