mysql 0开头字符串,mysql匹配字符串与表中字符串的开头

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

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?

解决方案

Try this:

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.

Also note that some characters in value (e.g. %) may be interpreted by LIKE as having a special meaning, which may undesirable.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值