开发者

MySQL string selecting commands

开发者 https://www.devze.com 2023-04-04 04:02 出处:网络
How can I select a row specific by string values SELECT * FROM my_table WHERE name= -- then some function to test what string it starts with

How can I select a row specific by string values

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

Its a bit hard to explain so i will explain an example with JavaScript

if(mystring.inde开发者_JS百科xOf('targetstring') != -1){
// if that variable contains this string

}

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

}

So all in all, I want to select rows, when their name(a string column) starts with or contains a specific string.


You can use LIKE and the wildcard %:

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

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


SELECT * FROM my_table WHERE name like "%string%"
0

精彩评论

暂无评论...
验证码 换一张
取 消