I am trying to pull data using an mysql query that requires that I look for keywords within a field in the database.
A user in my database may have the following in their ‘subjects’ field:
‘math | science | reading | writing’
This means that user accepts all of those subjects. This table is called ‘user_teacher’
I have another table for students (called ‘student_requests’) that consists of requests from students for say, science, or math, or maybe even two subjects (so the entry in this case would look like ‘science | math’). This table stores those values also in a field called ‘subjects’.
My question is, how would I form a query where i am looking up entries in ‘student_requests’ where the subject matches any of the listed subjects within a particular user’s profile from the table ‘user_teacher’? Let’s assume that i originally look up the teacher’s subject field using something like this:
$query = "SELECT * FROM user_teacher WHERE username = '$username' LIMIT 1 ";
$result = mysql_query($query);
while ($row = mysql_fetch_object($result)) {
$teacher_subjects=$row->subject;
}
In other words, if i am a teacher who offers reading and math, i want to see all entries in the ‘student_requests’ table where the subject field has ‘math’ or ‘reading’ in it.
Any assistance on this would be greatly appreciated. Also, if you think I am approaching this problem incorrectly based on how i have set up the tables, please let me know. My goal here is to have the queries run as quickly as possible.
Thanks!
You could try not using that kind of construction for you tables, instead make another table
subjects(id,name) and other two tablesstudents_requests(id,id_student,id_subject) [here you can record multiple requests for one student], andteach_subjects(id,id_teach,suject_id) [the same, multiple subject for a teacher] and yhen you can make a join, or even a plain php check between tables, based on what you received.EDIT 1:
And please change
id_subjectfromstudents_requestsinsubject_id.