【问题标题】:MySQL Convert a Substring_index and then check if it equals a value in another columnMySQL 转换一个 Substring_index,然后检查它是否等于另一列中的值
【发布时间】:2021-04-28 18:22:09
【问题描述】:

我有一列包含 12345 格式的 tibcoofferid。 另一列包含 Add_12345 格式的 tibcoaddevent。

我想要一个输出列来检查 tibcoofferid 是否等于 tibcoaddevent 中 _ 之后的数字。

我尝试了以下操作,但 AddEvent 检查被标记为 False,即使两个值匹配。谁能帮忙指出我做错了什么?

SELECT tibcoofferid, tibcoaddevent
    , CONVERT(SUBSTRING_INDEX(tibcoaddevent,'_',-1),UNSIGNED INTEGER) AS num
    , IF (tibcoofferid = 'num', 'True', 'False') AS AddEvent
FROM offer_360 
WHERE tibcoofferid IN (13979, 13980, 13900, 13352, 12709)

输出:

tibcoofferid tibcoaddevent num AddEvent
12709 Add_12709 12709 False
13352 Add_13352 13352 False
13900 Add_13900 13900 False
13979 Add_13979 13979 False
13980 Add_13980 13980 False

另外,是否可以在不输出“num”列的情况下执行此操作?

【问题讨论】:

    标签: mysql if-statement data-conversion


    【解决方案1】:

    将逻辑改写为tibcoaddevent = Add_<tibcoofferid>

    SELECT
      tibcoofferid,
      tibcoaddevent,
      case when tibcoaddevent = concat('Add_', tibcoofferid) then 'True' else 'False' end AS AddEvent
    FROM offer_360 
    WHERE ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 2021-03-08
      • 2014-01-30
      • 2017-09-23
      相关资源
      最近更新 更多