【发布时间】:2016-06-17 16:17:15
【问题描述】:
我需要从列中找到一个特定的数字,比如我在表 tours 中查找数字 9,该表有一个列 city。 city 列有一串数字,如
9
49
5,9
4,94
5,8,89,32
我需要在其中找到数字 9,所以我想要的唯一结果是
9
5,9
我曾尝试使用 REGEX,但无法正确使用。谁能指出我正确的方向?这是到目前为止的查询
SELECT
a.title, a.tourprice, a.city, b.city AS destCity
FROM
tours a
RIGHT JOIN
(SELECT
id, city
FROM
destinations
WHERE
id = 47) b ON a.city LIKE CONCAT('%,', b.city) OR a.city LIKE CONCAT(b.city, ',%') //b.city evaluates to 9
我知道在我想要的数字之前或之后可能总是有逗号,也可能不总是逗号。
【问题讨论】:
-
为什么不使用
WHERE a.city LIKE '9,%' OR a.city LIKE '%,9,%' OR a.city LIKE '%,9'?否则,我猜你可能会使用WHERE a.city REGEXP '[[:<:]]9[[:>:]]'。 我尝试过使用 REGEX,但无法正确使用。 - 你尝试了什么正则表达式? -
你真的应该考虑properly normalising that table。