User12345
User12345

Reputation: 5480

correct method to Use sub string in where clause mysql query

I have a table in mysql with column called type. contents are below

Type
Test 12
Test abc
start 1
start abcd
end 123

Now I want to select records where type starts with Test

Expected result:

Test 12
Test abc

But I am getting either

Test 12 or empty results

I have tried like below:

select * from table where type = 'Test 12'
select * from table where type = '%Test%'
select * from table where type = '%Test'

What should be the correct sql statement.

Upvotes: 1

Views: 237

Answers (4)

tadman
tadman

Reputation: 211680

If you want to do partial matches you need the LIKE operator:

SELECT * FROM table WHERE type LIKE 'Test%'

Upvotes: 2

Jacques Amar
Jacques Amar

Reputation: 1833

Sooo close! You want to start with the string, then don't use % in the start, and use LIKE instead of =

select * from table where type LIKE 'Test%'

Upvotes: 1

Mureinik
Mureinik

Reputation: 311883

The equality (=) operator doesn't accept wild cards. You should use the like operator instead:

SELECT * FROM table WHERE type LIKE 'Test%'

Upvotes: 1

isaace
isaace

Reputation: 3429

Use the Like keyword

select * from table where type LIKE 'Test%'

Upvotes: 1

Related Questions