Yogesh Rawal
Yogesh Rawal

Reputation: 105

Extract the single value from a column containing multiple values using PHP/MySQL

I have inserted multiple values from select statement in a single column using implode function. Now I want retrieve those values separately for my further queries. I have a student table in which I am storing their different area of interest in a single column using the implode function implode(',', $_POST['aoi']);. The structure of the table is like this:

id   |  Name   |           AOI
-------------------------------------------
101  | Paul    | Hardware,Networking
102  | Fred    | Finance,Insurance,Banking

Now I want to retrieve this student data separately based on their interest. I tried to extract data using explode function. But it is returning the data in array format.

And I have to extract area of interest separately. Can anybody tell me how to extract the single value from a column containing multiple value. So that I can execute my further queries.

Like $query = "select student.id,student.Name from student where AOI = 'Insurance'";

Thanks.

Upvotes: 0

Views: 1129

Answers (1)

user399666
user399666

Reputation: 19879

Your database structure is fundamentally wrong and it will most likely lead to scalability issues further down the line. You should look into the concept of database normalization. In this particular scenario, you should be using a many to many relationship:

Students:

id   | name  
---------------
101  | Paul    
102  | Fred    

StudentInterests

id   |  student_id  | interest_id
---------------------------------
1    |  101         | 1
2    |  101         | 2
3    |  102         | 1

Interests

id   | interest
----------------
1    | Insurance
2    | Networkings

Upvotes: 5

Related Questions