使用LIKE运算符的Select语句中的MySQL案例 [英] MySQL Case in Select Statement with LIKE operator

查看:115
本文介绍了使用LIKE运算符的Select语句中的MySQL案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在MySQL SELECT语句中组合CASE语句和LIKE运算符?

例如,我正在尝试查询一种以两种格式中的任何一种将数据存储在单列中的数据库(这很糟糕,伤了我的头脑,但我无法更改数据,所以它就是它的样子.).因此,有时numbers列将具有诸如"6901xxxxxxxx"之类的数据,有时其列将具有诸如"91xxxxxxxxxxxxxx"的数据.

我想做的就是这样查询数据-

SELECT
    CASE digits
      WHEN LIKE "6901%" THEN substr(digits,4)
      WHEN LIKE "91%" THEN substr(digits,2)
    END as "digits",
FROM raw

这显然行不通,但我希望有可能.

解决方案

使用

What I would like to do is query the data like so -

SELECT
    CASE digits
      WHEN LIKE "6901%" THEN substr(digits,4)
      WHEN LIKE "91%" THEN substr(digits,2)
    END as "digits",
FROM raw

This obviously doesn't work but Im hoping its possible.

Using the second form of CASE should work:

SELECT
  CASE
    WHEN digits LIKE '6901%' THEN substr(digits,4)
    WHEN digits LIKE '91%' THEN substr(digits,2)
  END as digits
FROM raw

Furthermore, you have a stray comma at the end of your SELECT.

这篇关于使用LIKE运算符的Select语句中的MySQL案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆