Drake
Drake

Reputation: 3891

MySQL string selecting commands

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.indexOf('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.

Upvotes: 0

Views: 336

Answers (2)

bfavaretto
bfavaretto

Reputation: 71908

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*/ 

Upvotes: 4

ennuikiller
ennuikiller

Reputation: 46965

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

Upvotes: 2

Related Questions