MySQL字符串选择命令

时间:2011-09-10 16:36:06

标签: mysql sql string function

如何选择字符串值特定的行

SELECT * FROM my_table WHERE name=
   -- then some function to test what string it starts with

有点难以解释所以我将用JavaScript解释一个例子

if(mystring.indexOf('targetstring') != -1){
// if that variable contains this string

}

if(mystring.indexOf('targetstring') == 0){
// if that variable starts with this string

}

总而言之,我想在他们的名字(字符串列)以或包含特定字符串开头时选择行。

2 个答案:

答案 0 :(得分:4)

您可以使用LIKE和通配符%

SELECT * FROM my_table WHERE name LIKE 'john%'; /* name starts with john*/ 

SELECT * FROM my_table WHERE name LIKE '%john%'; /* name contains john*/ 

答案 1 :(得分:2)

SELECT * FROM my_table WHERE name like "%string%"
相关问题