在 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
?