mysql匹配字符串与表中字符串的开头 [英] mysql match string with start of string in table
问题描述
我意识到,如果在创建表时可以对其进行修改,会容易得多,但是假设我做不到,那么我的表将如下所示:
I realise that it would be a lot easier if I could modify the table when it was created, but assuming I can't, I have a table that is such as:
abcd
abde
abdf
abff
bbsdf
bcggs
... snip large amount
zza
表中的值不是固定长度. 我有一个要匹配的字符串,例如abffagpokejfkjs. 如果相反,我可以做
The values in the table are not fixed length. I have a string to match such as abffagpokejfkjs . If it was the other way round, I could do
SELECT * from table where value like 'abff%'
但是我需要选择与所提供的字符串开头匹配的值.
but I need to select the value that matches the start of a string that is provided.
是否有一种快速的方法,还是需要在桌子上烦躁才能找到匹配项?
Is there a quick way of doing that, or does it need an itteration through the table to find a match?
推荐答案
尝试一下:
SELECT col1, col2 -- etc...
FROM your_table
WHERE 'abffagpokejfkjs' LIKE CONCAT(value, '%')
请注意,这将无法有效地使用索引,因此如果您有很多记录,它将变慢.
Note that this will not use an index effectively so it will be slow if you have a lot of records.
还要注意,value
中的某些字符(例如%
)可能被LIKE解释为具有特殊含义,这可能是不希望的.
Also note that some characters in value
(e.g. %
) may be interpreted by LIKE as having a special meaning, which may undesirable.
这篇关于mysql匹配字符串与表中字符串的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!