I have a table called "scholarships" that has a field named "majors" which contains comma separated major names for the majors the scholarship is related too.
Let's say the field could contain one (or more separated by commas) of the following: business, agribusiness, business administration, international business.
If someone is searching "business" as the major, how can I select "business" as a match but not the others?
The closest I've come is this but I know it could be better - my regex ability isn't so hot.
SELECT scholarship_id, scholarship_award_name, scholarship_majors
FROM scholarships
WHERE scholarship_majors rlike '[, ][[:<:]]business[[:>:]][, ]'
OR scholarship_majors rlike '^[[:<:]]business[[:>:]][, ]'
OR scholarship_majors rlike '[, ][[:<:]]business[[:>:]]$'
I'm trying to catch the field if it begins with "business" or ", business" or "business ", "business, " but not "business administration" etc...
Any advice?
Any advice?
Don't store the data in comma separated lists - that's denormalized data, and besides being difficult to isolate specifics, is also prone to bad data (typos, cases sensitivity...).
Define a
MAJORStable:Use a many-to-many table to join scholarships to one or more majors:
SCHOLARSHIP_MAJORS
Use JOINs to get scholarships based on majors:
...if you want the majors output in a comma separated list, use the GROUP_CONCAT function: