Skip to content
Advertisement

How to select data using like with out consider after character in laravel or sql

I have a table call labels_table.


id label
1 A_1_1
2 A_1_2
3 A_2_1
4 A_4_1
5 A_10_1
6 A_11_1

I need to select all the labels for A_1 group. A_1_1 and A_1_2 should be the correct labels.

I used the following query.

sql

SELECT * from `label_table` where `label` LIKE `A_1_%`;

Larval

$identifier = “A_1”;


$this->model->where('label', 'LIKE',$identifier.'_%')->get();

But both SQL and laravel queries returns all A_1_1, A_1_2, A_10_1 and A_11_1. But I need to get only A_1_1 and A_1_2.

Please explain what’s the issue here and give me a solution. thank you.

Advertisement

Answer

LIKE allows you to escape the wildcard characters. The default is backslash:

where `label` LIKE 'A_1_%';

However, you can also set your own:

where `label` LIKE 'A$_1$_%' ESCAPE '$';

Or use regular expressions:

where label REGEXP '^A_1_'
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement