Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a table ida idb name
1|2|san
2|2|bang1
3|3|spam
4|2|23andme

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%'
Posted
Updated 15-Sep-22 1:55am
Comments
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
SQL
create table `table` (ida int, idb int, `name` varchar(50));
insert into `table` (ida, idb, `name`) values
(1,2,'san'),
(2,2,'bang1'),
(3,3,'spam'),
(4,2,'23andme');
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
SQL
SELECT * FROM `table` WHERE '00123andme' LIKE CONCAT('%',`name`,'%');
You can read up on pattern matching at MySQL :: MySQL 8.0 Reference Manual :: 3.3.4.7 Pattern Matching[^]

Kudos to @0X01AA for their comment about LOCATE
These queries also return the rows you mention
SQL
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