我正在使用MySQL 5.5
。
我有一个名为nutritions
的表,其中包含一个带有text
数据类型的列serving_data
。
serving_data
列中的一些值如下:
[{"label":"1 3\/4 cups","unit":"3\/4 cups"},{"label":"1 cups","unit":"3\/4 cups"},{"label":"1 container (7 cups ea.)","unit":"3\/4 cups"}]
现在,我想查找包含像
1 3\/4杯
这样的serving_data
记录。为此,我已经做了一次查询,
SELECT id,`name`,`nutrition_data`,`serving_data`
FROM `nutritions` WHERE serving_data REGEXP '(\d\s\\\D\d\scup)+';
但似乎没有起作用。
我还尝试过
SELECT id,`name`,`nutrition_data`,`serving_data`
FROM `nutritions` WHERE serving_data REGEXP '/(\d\s\\\D\d\scup)+/g';
如果我在http://regexr.com/中使用相同的模式,看起来匹配成功了。
有人可以帮我吗?
\d
替换为[0-9]
,将\D
替换为[^0-9]
,将\s
替换为[[:space:]]
。尝试使用WHERE serving_data REGEXP '([0-9]+[[:space:]])?[0-9]+/[0-9]+[[:space:]]+cup';
。请参阅https://regex101.com/r/sO8fL3/1。 - Wiktor Stribiżew/.../g
,只使用模式。顺便问一下,字符串中是否真的有\
?我相信没有\
,对吗? - Wiktor Stribiżewname
,nutrition_data
,serving_data
FROMnutritions
WHERE serving_data REGEXP '[0-9][[:space:]]/[^0-9][0-9][[:space:]]cup'; - 看起来不行。是的,字符串中有\
。 - Dev[{"label":"1\/2 cup","unit":"cup"},{"label":"1 cup","unit":"cup"}]
这样的记录也会被该模式选中。我只想选择像1 3\/4 cups
这样的记录与该模式匹配。 - Dev?
-[0-9]+[[:space:]][0-9]+\\\\/[0-9]+[[:space:]]+cup
。 - Wiktor Stribiżew