Click here to Skip to main content
15,791,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a table ida idb name

now i want to make a query to find result 4 with name 00123andme
or i want to get result id 2 searching with 001bang1

What I have tried:

SELECT * FROM `table` WHERE `name` LIKE '%00123andme%'
Updated 15-Sep-22 2:55am
0x01AA 14-Sep-22 14:46pm    
On a first glance it looks like LOCATE should do the job: LOCATE() function in MySQL - GeeksforGeeks[^]

1 solution

I was going to share a fiddle but I can't just now.
I created your table like this
create table `table` (ida int, idb int, `name` varchar(50));
insert into `table` (ida, idb, `name`) values
First comment - I would avoid using reserved words such as table or name

The reason your attempt doesn't work is because it is the "wrong way around". Try this instead
SELECT * FROM `table` WHERE '00123andme' LIKE CONCAT('%',`name`,'%');
You can read up on pattern matching at MySQL :: MySQL 8.0 Reference Manual :: Pattern Matching[^]

Kudos to @0X01AA for their comment about LOCATE
These queries also return the rows you mention
select * from `table` where LOCATE(`name`,'00123andme') > 0;
select * from `table` where LOCATE(`name`,'001bang1') > 0;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900