我尝试过使用“LIKE”,但遇到了一些问题,我将在下面解释。
我有一个看起来像其中任何一个的字符串列。 “1010、2020、3030” “1010” ""
我希望能够查看此字符串是否包含单个 ID。例如 2020。如果确实如此,则返回该行。我尝试使用 like,但如果 id 为 20,它将返回该行,因为 2020 包含 20。
选择整个数据库然后使用定界符遍历所有字符串将花费太多时间。是否可以实现?
最佳答案
这就是您不在单个字段中存储多个值的原因。因为你的设计不好,这是你每次都必须使用的查询结构来补偿它:
WHERE
foo = 2020 // exact match, only value in field
OR foo LIKE '2020,%' // value is at start of field
OR foo LIKE '%,2020,%' // value is somewhere in the middle of the field
OR foo LIKE '%,2020' // value is at the end of the field
或者您可以有一个适当规范化的设计,然后就完成了
WHERE childtable.foo = 2020
并完成它。
关于MySQL选择列包含值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25022399/