regex - MYSQL REGEXP query for keywords column -
i need regular expression users have keyword 52 , users have keyword 52,53 not 54. below table structure
create table `user` ( `id` int not null auto_increment, `first_name` varchar(100) not null, `last_name` varchar(100) not null, `keywords` text, primary key (`id`) ) engine=innodb default charset=utf8; sample record1: 100, sam, thompson, "50,51,52,53,54" sample record2: 100, wan, thompson, "50,52,53" sample record3: 100, kan, thompson, "53,52,50,54" 50 = sports 51 = cricket 52 = soccer 53 = baseball 54 = tennis
so far query have come with. gives records 3.
select * `user` keywords regexp '[[:<:]]52,53,54[[:>:]]'
try using find_in_set()
rather complex regular expression:
select u.* user text = '52' or (find_in_set('52', text) > 0 , find_in_set('53', text) > 0 , find_in_set('54', text) = 0)
explanation:
where text = '52' -- users have keyword 52 , keyword find_in_set('52', text) > 0 , find_in_set('53', text) > 0 , find_in_set('54', text) = 0 -- users have keywords 52 , 53 not 54
Comments
Post a Comment