1

I have data like this student_id=1 and class_id=1,2,3 (class id can be multiple) in my controller.

And i want result in mysql table like this:

nimesh
  • 45
  • 2
  • 9
  • Make a new middle class/table Student-Class and put the student_id and class_id how attributes, at this table you can save time, signature, etc.. what you need of the class-student – Jordi Jordi Apr 12 '18 at 09:13
  • Are you looking for an asp answer? – P.Salmon Apr 12 '18 at 09:29
  • how can i do partition of class_id(1,2,3) using sql statement and insert into table with different rows.Can u help me? :) – nimesh Apr 12 '18 at 11:26

2 Answers2

1

Make a new middle-table called studentclass with Id being the primary key and Student_Id and Class_Id as foreign keys pointing to their tables. You can then store the Id of studentclass as foreign key in the students table.

Here's an example for better understanding:

Student Id (Primary) StudentClass_Id (foreign key)

StudentClass Id (Primary) Student_Id (Foreign key) Class_Id (Foreign key)

Heimi
  • 126
  • 1
  • 12
  • student_id and class_id both are foreign keys from reference table student and class.Now i want to insert multiple class_id with same student id into table with format i have shown in image. – nimesh Apr 12 '18 at 09:36
  • Then good luck implementing Unnikrishnan Rs answer and consider that your db will be in 2nd normal form. – Heimi Apr 12 '18 at 09:42
  • can we do partition on class_id(1,2,3) from sql statement and insert into table with different rows? – nimesh Apr 12 '18 at 11:43
  • how do you mean that? You're maybe looking for this [How can I simulate an array variable in MySQL?](https://stackoverflow.com/questions/12176709/how-can-i-simulate-an-array-variable-in-mysql) – Heimi Apr 12 '18 at 11:45
0

You should use any functions like split to make an array from string separated by comma. And you can insert the row in MySQL through looping the array

Unnikrishnan R
  • 146
  • 1
  • 6
  • It can be done using array and implementing loop but can we do this from mysql statement like seperating class_id(1,2,3) and insert it into table with different rows? If yes then guide me.Thankyou – nimesh Apr 12 '18 at 11:37