Mysql 查找具有最长字段值的行作为文本的开头
Mysql find row with longest field value as beginning of a text
我在 mysql table 中有类似的东西。
ID Text
1 a
2 b
3 a-b
4 b-a
5 a-b-c
6 a-b-d
通常你在一个列中搜索文本,我只是想要相反的东西,我想要一个查询来搜索一个文本中的所有列。不知道可不可以。
if text is "a-b-f" It must return query with ID 3 ("a-b")
if text is "a-c" It must return query with ID 1 ("a")
if text is "b-a" It must return query with ID 2 ("b")
if text is "b-b" It must return query with ID 2 ("b")
if text is "a-b-c-d-e-f" It must return query with ID 5 ("a-b-c")
if text is "c-a-a" It must return an empty query
感谢您的帮助。
PD:我一直在寻找类似的东西,但我找到的所有方法都是在列值内搜索文本,而这与我所说的相反。
可以像
一样使用
select ID from my_table
where 'your_text_seach' like concat ( text, '%')
order by length(text) desc
limit 1
我在 mysql table 中有类似的东西。
ID Text
1 a
2 b
3 a-b
4 b-a
5 a-b-c
6 a-b-d
通常你在一个列中搜索文本,我只是想要相反的东西,我想要一个查询来搜索一个文本中的所有列。不知道可不可以。
if text is "a-b-f" It must return query with ID 3 ("a-b")
if text is "a-c" It must return query with ID 1 ("a")
if text is "b-a" It must return query with ID 2 ("b")
if text is "b-b" It must return query with ID 2 ("b")
if text is "a-b-c-d-e-f" It must return query with ID 5 ("a-b-c")
if text is "c-a-a" It must return an empty query
感谢您的帮助。
PD:我一直在寻找类似的东西,但我找到的所有方法都是在列值内搜索文本,而这与我所说的相反。
可以像
一样使用 select ID from my_table
where 'your_text_seach' like concat ( text, '%')
order by length(text) desc
limit 1