使用LIKE运算符的Select语句中的MySQL案例 [英] MySQL Case in Select Statement with LIKE operator
问题描述
是否可以在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 - This obviously doesn't work but Im hoping its possible. Using the second form of Furthermore, you have a stray comma at the end of your 这篇关于使用LIKE运算符的Select语句中的MySQL案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!SELECT
CASE digits
WHEN LIKE "6901%" THEN substr(digits,4)
WHEN LIKE "91%" THEN substr(digits,2)
END as "digits",
FROM raw
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
SELECT
.