MariaDB不获取枚举的值,而只获取枚举的顺序

问题描述 投票:0回答:1

在 MariaDB sql 中:

CREATE TABLE `sales1_plan_detail` (
  `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `plan_duration` enum('12') NOT NULL,
  PRIMARY KEY (`id`));

INSERT INTO `sales1_plan_detail` (`id`, `plan_duration`) VALUES (1, '12');

CREATE TABLE `sales1_plan_country` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `common_countryId` smallint(5) unsigned DEFAULT NULL,
  `plan_detailId` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `common_countryId_sales1_plan_detailId` (`common_countryId`,`plan_detailId`),
  KEY `sales1_plan_detailId` (`plan_detailId`),
  CONSTRAINT `sales1_plan_country_ibfk_1` FOREIGN KEY (`plan_detailId`) REFERENCES `sales1_plan_detail` (`id`) ON UPDATE CASCADE);

INSERT INTO `sales1_plan_country` (`id`, `common_countryId`, `plan_detailId`) VALUES (1, 1, 1);

CREATE TABLE `sales1_user_plan` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `sales_userId` int(10) unsigned NOT NULL,
  `plan_countryId` smallint(5) unsigned NOT NULL,
  `plan_start` date NOT NULL,
  PRIMARY KEY (`id`),
  KEY `plan_countryId` (`plan_countryId`),
  KEY `sales_userId` (`sales_userId`),
  CONSTRAINT `sales1_user_plan_ibfk_2` FOREIGN KEY (`plan_countryId`) REFERENCES `sales1_plan_country` (`id`) ON UPDATE CASCADE);

INSERT INTO `sales1_user_plan` (`id`, `sales_userId`, `plan_countryId`, `plan_start`) VALUES
(1, 1, 1, '2024-03-03');

我使用这个sql:

SELECT 
    DATE_ADD(sup.plan_start, INTERVAL CAST(spd.plan_duration AS UNSIGNED) MONTH) AS plan_expiry_date
FROM 
    sales1_user_plan sup
JOIN 
    sales1_plan_country spc ON sup.plan_countryId = spc.id
JOIN 
    sales1_plan_detail spd ON spc.plan_detailId = spd.id
WHERE 
    sup.sales_userId = 1
ORDER BY 
    sup.plan_start DESC
LIMIT 1;

它给了我:

plan_expiry_date
2024-04-03
,而它应该给我
plan_expiry_date
2025-03-03

因为它获取

plan_duration enum('12')
作为枚举的索引
1
,而不是作为枚举中的值
12

如何告诉 SQL 获取枚举的值

12
,而不是枚举中的索引
1

sql mariadb
1个回答
0
投票

根据 mariadb 文档。这里

我们不建议使用ENUM来存储数字。

因此存储的数值将作为枚举索引获取

© www.soinside.com 2019 - 2024. All rights reserved.