在 mysql 中使用 between 运算符的字符串值

using string values for between operator in mysql

我有一个 table sales 具有 salesId,salesDate,..etc 的属性。 salesId 列是 varchar。我需要根据 [=24= 的条件将 salesDateyear 值连接到 salesId ] 的 salesDate。我需要对大约 100 个 salesID 的范围执行此操作。 例如:old salesId = 7 和相应的 salesDate = '2018-05-07' 然后需要 new salesId = '7/2018-2019' 所以我尝试了以下方法:

    update sales
set salesId = case
                when month(salesDate)>=4 then concat(salesId,concat("/",year(salesDate),"-",year(salesDate)+1))
                else
                    concat(salesId,concat("/",year(salesDate)-1,"-",year(salesDate)))
                end
where cast(salesId as unsigned) between "7" and "10";

但是,我收到以下错误:

Error Code: 1292. Truncated incorrect INTEGER value: '1/17-18'

我什至尝试过不使用任何 Cast() 作为

    update sales
set salesId = case
                when month(salesDate)>=4 then concat(salesId,concat("/",year(salesDate),"-",year(salesDate)+1))
                else
                    concat(salesId,concat("/",year(salesDate)-1,"-",year(salesDate)))
                end
where salesId between "7" and "10";  

但在这种情况下,查询运行正常,但我得到:

0 row(s) affected Rows matched: 0  Changed: 0  Warnings: 0

我无法找出错误或如何解决 proceed.Could 有人请提供一些指导吗? 谢谢。 样本数据

salesId salesDate
7          2017-05-15
8          2017-06-16
9          2017-07-18
10         2017-08-20

...

Required Result

 salesId                  salesDate
    7/2017-2018         2017-05-15
    8/2017-2018         2017-06-16
    9/2017-2018         2017-07-18
    10/2016-2017        2017-02-20

字段salesId必须是类似

的文本

您必须更改您的更新查询以包含已更新的列,以便 SUBSTRING_INDEX 仅包含数字

 update sales
set salesId = case
                when month(salesDate)>=4 then concat(SUBSTRING_INDEX(salesId,'/',1),concat("/",year(salesDate),"-",year(salesDate)+1))
                else
                    concat(SUBSTRING_INDEX(salesId,'/',1),concat("/",year(salesDate)-1,"-",year(salesDate)))
                end
where cast(SUBSTRING_INDEX(salesId,'/',1) as unsigned) between "7" and "10";

这样你的更新功能才能正常工作

CREATE TABLE sales (
  `salesId` VARCHAR(20),
  `salesDate` VARCHAR(10)
);

INSERT INTO sales
  (`salesId`, `salesDate`)
VALUES
  ('7', '2017-05-15'),
  ('8', '2017-06-16'),
  ('9', '2017-07-18'),
  ('10', '2017-08-20');
✓

✓
    update sales
set salesId = case
                when month(salesDate)>=4 then concat(salesId,concat("/",year(salesDate),"-",year(salesDate)+1))
                else
                    concat(salesId,concat("/",year(salesDate)-1,"-",year(salesDate)))
                end
where cast(salesId as unsigned) between "7" and "10";
SELECT *  from sales
salesId      | salesDate 
:----------- | :---------
7/2017-2018  | 2017-05-15
8/2017-2018  | 2017-06-16
9/2017-2018  | 2017-07-18
10/2017-2018 | 2017-08-20
 update sales
set salesId = case
                when month(salesDate)>=4 then concat(SUBSTRING_INDEX(salesId,'/',1),concat("/",year(salesDate),"-",year(salesDate)+1))
                else
                    concat(SUBSTRING_INDEX(salesId,'/',1),concat("/",year(salesDate)-1,"-",year(salesDate)))
                end
where cast(SUBSTRING_INDEX(salesId,'/',1) as unsigned) between "7" and "10";
SELECT *  from sales
salesId      | salesDate 
:----------- | :---------
7/2017-2018  | 2017-05-15
8/2017-2018  | 2017-06-16
9/2017-2018  | 2017-07-18
10/2017-2018 | 2017-08-20

db<>fiddle here

我之前遇到的错误实际上是 here 所述的警告。因此,在使用更新忽略后,我能够执行查询以获得预期结果。 但是我会等待一个更全面的答案来让它工作而不需要忽略。 谢谢