簡體   English   中英

用例創建新列MySQL

[英]Using case to create a new column MySQL

我是SQL的新手,正在嘗試使用另一列中的值創建新列。

我正在使用這樣的東西。

Select 
a.idclientecrm,
(case
when a.titulo like '%Mensual'       then 'Mensual'
when a.titulo like '%Trimestral'    then 'Trimestral'
when a.titulo like '%Semestral'     then 'Semestral'
when a.titulo like '%Anual'         then 'Anual'
else null
end) as Periodo_Producto
from tareas a

當我發現這個詞Mensuala.titulo我需要在新柱等單詞“Mensual”為每一個不同的時間段(trimestral,semestralanual)。

“喜歡”部分似乎不起作用,我也嘗試過使用contains,但是我也無法使其起作用。

希望這是可以理解的。

在比較字符串的末尾(以及開始處),您需要一個“%”符號以進行比較,以檢查字符串中的任何位置-目前,僅檢查字符串是否以時間段單詞結尾 嘗試:

Select 
a.idclientecrm,
(case
when a.titulo like '%Mensual%'       then 'Mensual'
when a.titulo like '%Trimestral%'    then 'Trimestral'
when a.titulo like '%Semestral%'     then 'Semestral'
when a.titulo like '%Anual%'         then 'Anual'
end) as Periodo_Producto
from tareas a

(另外, else null也是不必要的-默認情況下,在沒有匹配條件的case返回null。)

%放在String兩端

   Select 
    a.idclientecrm,
    (case
    when a.titulo like '%Mensual%'       then 'Mensual'
    when a.titulo like '%Trimestral%'    then 'Trimestral'
    when a.titulo like '%Semestral%'     then 'Semestral'
    when a.titulo like '%Anual%'         then 'Anual'
    else null
    end) as Periodo_Producto
    from tareas a

在兩端使用%。

嘗試這個:

Select 
a.idclientecrm,
(case
when a.titulo like '%Mensual%'       then 'Mensual'
when a.titulo like '%Trimestral%'    then 'Trimestral'
when a.titulo like '%Semestral%'     then 'Semestral'
when a.titulo like '%Anual%'         then 'Anual'
else null
end) as Periodo_Producto
from tareas a

這是一個小提琴

希望這可以根據您的需要工作。 謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM