i have one query that need some changes, and i don’t get any clue to do this :
this is my query :
select * from user_data a
left join user_group b
on (a.role like b.role)
actually role value in userdata is (varchar)‘staff’
and role value in group is (varchar)‘staff;security;finance’
so i don’t get result what i expected ..
i imagine the query should be similar to this :
select * from user_data a
left join user_group b
on (b.role like a.role+";%") // using wildcard
and i still don’t know the right query using wildcard to this case
any one can help?
You can use
CONCAT:Note – does
b.roleonly have to matcha.roleat the beginning? what if it wassecurity;staff;finance? You could doCONCAT('%',a.role,'%').You could do
CONCAT('%','a.role','%')to handle matchinga.roleat any position, but only if you can be sure that you won’t have nested roles.For example: if
b.roleisstaffanda.roleisfinance;gardenstaff;security, then this row will be returned from the query even though the role isgardenstaffand not staff.As an alternative, you can use
RLIKEinstead ofLIKE. This is basically a regular-expressions verson ofLIKE.In particular, the regex
[[:<:]]staff[[:>:]]will match the whole wordstaff. The[[:<:]]and[[:>:]]stand for word boundaries, which stop you from matching thestaffingardenstaff.So, your query could be:
And this would work for
b.rolebeing anywhere in the semicolon-separateda.role.