SQL Query for subject prerequisiteHow can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to query MongoDB with “like”?How to import an SQL file using the command line in MySQL?Select and display all rows belonging to a specific IDHow to update row in junction table for student gradeHow to create Student Subject with prerequisite Database

Is there any other simpler way to draw the following cross section?

Why can't argument be forwarded inside lambda without mutable?

How long does it take a postcard to get from USA to Germany?

Emergency stop in plain TeX, pdfTeX, XeTeX and LuaTeX?

Is throwing dice a stochastic or a deterministic process?

What is more safe for browsing the web: PC or smartphone?

HSA - Continue to Invest?

Picking a theme as a discovery writer

Why are condenser mics so much more expensive than dynamics?

How to speed up large double sums in a table?

GitLab account hacked and repo wiped

What does the phrase "go for the pin" mean here?

How to detect nM levels of Copper(I) Oxide in blood?

If I no longer control a permanent when it would be sacrificed, is it still sacrificed?

Can an earth elemental drag a tiny creature underground with Earth Glide?

Why is the blank symbol not considered part of the input alphabet of a Turing machine?

Class Not Passing SObject By Reference

Can anyone identify this unknown 1988 PC card from The Palantir Corporation?

Endgame puzzle: How to avoid stalemate and win?

My large rocket is still flipping over

What are the requirements for a river delta to form?

Collision domain question

How do I, as a DM, handle a party that decides to set up an ambush in a dungeon?

Antivirus for Ubuntu 18.04



SQL Query for subject prerequisite


How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to query MongoDB with “like”?How to import an SQL file using the command line in MySQL?Select and display all rows belonging to a specific IDHow to update row in junction table for student gradeHow to create Student Subject with prerequisite Database






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Sorry i made another question because my last question is too confusing.



EDIT: My Current QUERY



UPDATE student_subject 
JOIN subject_bsit
ON subject_bsit.subject_id = student_subject.sub_id
SET enrolled = 1
where student_subject.student_id = 1235
and student_subject.sub_id = 1


What i want to happen is.



Student_id=1235 is trying to enroll subject_id=5 so that statement must not happen because subject_id=5 has a Prerequisite to subject_id=1 or programming 1



But if example in the student_subject sub_id 1 and Enrolled=1 meaning the student finish the programming and already enrolled now the student=1235 can now enroll the subjectid=5



I don't have any idea how to this query . Thank you so much.



THIS IS THE TABLE

"student"

-----------------------
|studentID | FullName |
-----------------------
|1234 | John |
|1235 | Michael |
|1236 | Bryce |

"subject_bsit"

-----------------------------------------
|subject_id| subject_name | pre_id |
-----------------------------------------
| 1 | Programming 1 | 0 |
| 2 | Networking | 0 |
| 3 | Algorithm | 0 |
| 4 | Physical Educ | 0 |
| 5 | Programming 2 | 1 |

This is the Junction table to connect the
two now.

"student_subject"

------------------------------------------------
| student_id | subject_id | Grade | Enrolled |
------------------------------------------------
| 1235 | 1 | 0 | 0 |
| 1235 | 2 | 0 | 0 |
| 1235 | 3 | 0 | 0 |
| 1234 | 1 | 0 | 0 |









share|improve this question
























  • Think about what it is that you want to get. You want the records from student_subject where the student_id matches your student and Enrolled is 1 and the subject_id is equal to the pre_id of the subject_bsit where the subject_id is the one for the subject you want to enrol in. If you're having trouble writing a SQL query then it's usually because you don't have a clear idea of what the query has to do, so work that out first. Pick up a pen and paper and write down, in plain language. You can then write code to implement it bit by bit.

    – jmcilhinney
    Mar 23 at 4:29











  • Hi, I already have my SQL query but i am having a problem because of the junction table and too many connection i will update it with my query.

    – Kimberypalet
    Mar 23 at 4:31











  • And delete one or other of your questions.

    – Strawberry
    Mar 23 at 7:45

















0















Sorry i made another question because my last question is too confusing.



EDIT: My Current QUERY



UPDATE student_subject 
JOIN subject_bsit
ON subject_bsit.subject_id = student_subject.sub_id
SET enrolled = 1
where student_subject.student_id = 1235
and student_subject.sub_id = 1


What i want to happen is.



Student_id=1235 is trying to enroll subject_id=5 so that statement must not happen because subject_id=5 has a Prerequisite to subject_id=1 or programming 1



But if example in the student_subject sub_id 1 and Enrolled=1 meaning the student finish the programming and already enrolled now the student=1235 can now enroll the subjectid=5



I don't have any idea how to this query . Thank you so much.



THIS IS THE TABLE

"student"

-----------------------
|studentID | FullName |
-----------------------
|1234 | John |
|1235 | Michael |
|1236 | Bryce |

"subject_bsit"

-----------------------------------------
|subject_id| subject_name | pre_id |
-----------------------------------------
| 1 | Programming 1 | 0 |
| 2 | Networking | 0 |
| 3 | Algorithm | 0 |
| 4 | Physical Educ | 0 |
| 5 | Programming 2 | 1 |

This is the Junction table to connect the
two now.

"student_subject"

------------------------------------------------
| student_id | subject_id | Grade | Enrolled |
------------------------------------------------
| 1235 | 1 | 0 | 0 |
| 1235 | 2 | 0 | 0 |
| 1235 | 3 | 0 | 0 |
| 1234 | 1 | 0 | 0 |









share|improve this question
























  • Think about what it is that you want to get. You want the records from student_subject where the student_id matches your student and Enrolled is 1 and the subject_id is equal to the pre_id of the subject_bsit where the subject_id is the one for the subject you want to enrol in. If you're having trouble writing a SQL query then it's usually because you don't have a clear idea of what the query has to do, so work that out first. Pick up a pen and paper and write down, in plain language. You can then write code to implement it bit by bit.

    – jmcilhinney
    Mar 23 at 4:29











  • Hi, I already have my SQL query but i am having a problem because of the junction table and too many connection i will update it with my query.

    – Kimberypalet
    Mar 23 at 4:31











  • And delete one or other of your questions.

    – Strawberry
    Mar 23 at 7:45













0












0








0








Sorry i made another question because my last question is too confusing.



EDIT: My Current QUERY



UPDATE student_subject 
JOIN subject_bsit
ON subject_bsit.subject_id = student_subject.sub_id
SET enrolled = 1
where student_subject.student_id = 1235
and student_subject.sub_id = 1


What i want to happen is.



Student_id=1235 is trying to enroll subject_id=5 so that statement must not happen because subject_id=5 has a Prerequisite to subject_id=1 or programming 1



But if example in the student_subject sub_id 1 and Enrolled=1 meaning the student finish the programming and already enrolled now the student=1235 can now enroll the subjectid=5



I don't have any idea how to this query . Thank you so much.



THIS IS THE TABLE

"student"

-----------------------
|studentID | FullName |
-----------------------
|1234 | John |
|1235 | Michael |
|1236 | Bryce |

"subject_bsit"

-----------------------------------------
|subject_id| subject_name | pre_id |
-----------------------------------------
| 1 | Programming 1 | 0 |
| 2 | Networking | 0 |
| 3 | Algorithm | 0 |
| 4 | Physical Educ | 0 |
| 5 | Programming 2 | 1 |

This is the Junction table to connect the
two now.

"student_subject"

------------------------------------------------
| student_id | subject_id | Grade | Enrolled |
------------------------------------------------
| 1235 | 1 | 0 | 0 |
| 1235 | 2 | 0 | 0 |
| 1235 | 3 | 0 | 0 |
| 1234 | 1 | 0 | 0 |









share|improve this question
















Sorry i made another question because my last question is too confusing.



EDIT: My Current QUERY



UPDATE student_subject 
JOIN subject_bsit
ON subject_bsit.subject_id = student_subject.sub_id
SET enrolled = 1
where student_subject.student_id = 1235
and student_subject.sub_id = 1


What i want to happen is.



Student_id=1235 is trying to enroll subject_id=5 so that statement must not happen because subject_id=5 has a Prerequisite to subject_id=1 or programming 1



But if example in the student_subject sub_id 1 and Enrolled=1 meaning the student finish the programming and already enrolled now the student=1235 can now enroll the subjectid=5



I don't have any idea how to this query . Thank you so much.



THIS IS THE TABLE

"student"

-----------------------
|studentID | FullName |
-----------------------
|1234 | John |
|1235 | Michael |
|1236 | Bryce |

"subject_bsit"

-----------------------------------------
|subject_id| subject_name | pre_id |
-----------------------------------------
| 1 | Programming 1 | 0 |
| 2 | Networking | 0 |
| 3 | Algorithm | 0 |
| 4 | Physical Educ | 0 |
| 5 | Programming 2 | 1 |

This is the Junction table to connect the
two now.

"student_subject"

------------------------------------------------
| student_id | subject_id | Grade | Enrolled |
------------------------------------------------
| 1235 | 1 | 0 | 0 |
| 1235 | 2 | 0 | 0 |
| 1235 | 3 | 0 | 0 |
| 1234 | 1 | 0 | 0 |






mysql sql database vb.net phpmyadmin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 8:39









Strawberry

26.9k83250




26.9k83250










asked Mar 23 at 4:21









KimberypaletKimberypalet

486




486












  • Think about what it is that you want to get. You want the records from student_subject where the student_id matches your student and Enrolled is 1 and the subject_id is equal to the pre_id of the subject_bsit where the subject_id is the one for the subject you want to enrol in. If you're having trouble writing a SQL query then it's usually because you don't have a clear idea of what the query has to do, so work that out first. Pick up a pen and paper and write down, in plain language. You can then write code to implement it bit by bit.

    – jmcilhinney
    Mar 23 at 4:29











  • Hi, I already have my SQL query but i am having a problem because of the junction table and too many connection i will update it with my query.

    – Kimberypalet
    Mar 23 at 4:31











  • And delete one or other of your questions.

    – Strawberry
    Mar 23 at 7:45

















  • Think about what it is that you want to get. You want the records from student_subject where the student_id matches your student and Enrolled is 1 and the subject_id is equal to the pre_id of the subject_bsit where the subject_id is the one for the subject you want to enrol in. If you're having trouble writing a SQL query then it's usually because you don't have a clear idea of what the query has to do, so work that out first. Pick up a pen and paper and write down, in plain language. You can then write code to implement it bit by bit.

    – jmcilhinney
    Mar 23 at 4:29











  • Hi, I already have my SQL query but i am having a problem because of the junction table and too many connection i will update it with my query.

    – Kimberypalet
    Mar 23 at 4:31











  • And delete one or other of your questions.

    – Strawberry
    Mar 23 at 7:45
















Think about what it is that you want to get. You want the records from student_subject where the student_id matches your student and Enrolled is 1 and the subject_id is equal to the pre_id of the subject_bsit where the subject_id is the one for the subject you want to enrol in. If you're having trouble writing a SQL query then it's usually because you don't have a clear idea of what the query has to do, so work that out first. Pick up a pen and paper and write down, in plain language. You can then write code to implement it bit by bit.

– jmcilhinney
Mar 23 at 4:29





Think about what it is that you want to get. You want the records from student_subject where the student_id matches your student and Enrolled is 1 and the subject_id is equal to the pre_id of the subject_bsit where the subject_id is the one for the subject you want to enrol in. If you're having trouble writing a SQL query then it's usually because you don't have a clear idea of what the query has to do, so work that out first. Pick up a pen and paper and write down, in plain language. You can then write code to implement it bit by bit.

– jmcilhinney
Mar 23 at 4:29













Hi, I already have my SQL query but i am having a problem because of the junction table and too many connection i will update it with my query.

– Kimberypalet
Mar 23 at 4:31





Hi, I already have my SQL query but i am having a problem because of the junction table and too many connection i will update it with my query.

– Kimberypalet
Mar 23 at 4:31













And delete one or other of your questions.

– Strawberry
Mar 23 at 7:45





And delete one or other of your questions.

– Strawberry
Mar 23 at 7:45












1 Answer
1






active

oldest

votes


















1














Comments in line.



Private conString As String = "Your connection string"

Private Sub EnrollStudent(StudentID As Integer, SubjectID As Integer)
'Question Does Subject have a prerequisite?
Dim Prerequisite As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select pre_id from subject_bsit
where subject_id = @subject_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
Prerequisite = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - No, It is OK to enroll student there are no prerequisites
If Prerequisite = 0 Then
InsertEnrollment(StudentID, SubjectID)
Return
End If
'Answer - Yes there Is a prerequisite
'New Question - Has the student passed the prerequisite?
Dim PassingGrade As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select Grade From student_subject
Where subject_id = @subject_id
And student_id = @student_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = Prerequisite
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cn.Open()
PassingGrade = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - Yes, student has a passing grade.
If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)
Else 'Answer - No, student does not have a passing grade in prerequisite.
MessageBox.Show("Student cannot enroll because of prerequisite.")
End If
End Sub

Private Sub InsertEnrollment(StudentID As Integer, SubjectID As Integer)
Dim query = "Insert Into student_subject (student_id, subjectId, Grade, Enrolled)
Values (@student_id, @subject_id, 0, 1);"
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand(query, cn)
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Successful enrollment")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EnrollStudent(1235, 5)
End Sub


EDIT



Typo here...



If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)


This should be



If PassingGrade > 0 Then
InsertEnrollment(StudentID, SubjectID)


The second argument is SubjectID






share|improve this answer

























  • Hi, it is working if i only try to add a subject with no prerequisite. but i try to add the ID of subject that has a prereisite this is the error. Cannot add or update a child row: a foreign key constraint fails (csais.student_subject, CONSTRAINT student_subject_ibfk_2 FOREIGN KEY (sub_id) REFERENCES subject_bsit (subject_id))

    – Kimberypalet
    Apr 11 at 4:45











  • @Kimberypalet Very sorry, it was a typo. See the edit to my answer. If this works for you please accept my answer by clicking the check mark (tick mark) to the left of the answer.

    – Mary
    Apr 11 at 16:01











  • Hi. Thank you again. Can you help me again ?? I run on another problem. What if the passing grade is only 1,2 and 3 and 5 is equals to Failed and there is also another "INC" means incomplete so i need to set the passing grade as string but Cint function is only for Integer ?

    – Kimberypalet
    Apr 12 at 20:10











  • I suggest you accept my answer and ask a new question.

    – Mary
    Apr 12 at 21:07











  • Sorry. Thank you so much.

    – Kimberypalet
    Apr 12 at 21:28











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310588%2fsql-query-for-subject-prerequisite%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Comments in line.



Private conString As String = "Your connection string"

Private Sub EnrollStudent(StudentID As Integer, SubjectID As Integer)
'Question Does Subject have a prerequisite?
Dim Prerequisite As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select pre_id from subject_bsit
where subject_id = @subject_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
Prerequisite = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - No, It is OK to enroll student there are no prerequisites
If Prerequisite = 0 Then
InsertEnrollment(StudentID, SubjectID)
Return
End If
'Answer - Yes there Is a prerequisite
'New Question - Has the student passed the prerequisite?
Dim PassingGrade As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select Grade From student_subject
Where subject_id = @subject_id
And student_id = @student_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = Prerequisite
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cn.Open()
PassingGrade = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - Yes, student has a passing grade.
If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)
Else 'Answer - No, student does not have a passing grade in prerequisite.
MessageBox.Show("Student cannot enroll because of prerequisite.")
End If
End Sub

Private Sub InsertEnrollment(StudentID As Integer, SubjectID As Integer)
Dim query = "Insert Into student_subject (student_id, subjectId, Grade, Enrolled)
Values (@student_id, @subject_id, 0, 1);"
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand(query, cn)
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Successful enrollment")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EnrollStudent(1235, 5)
End Sub


EDIT



Typo here...



If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)


This should be



If PassingGrade > 0 Then
InsertEnrollment(StudentID, SubjectID)


The second argument is SubjectID






share|improve this answer

























  • Hi, it is working if i only try to add a subject with no prerequisite. but i try to add the ID of subject that has a prereisite this is the error. Cannot add or update a child row: a foreign key constraint fails (csais.student_subject, CONSTRAINT student_subject_ibfk_2 FOREIGN KEY (sub_id) REFERENCES subject_bsit (subject_id))

    – Kimberypalet
    Apr 11 at 4:45











  • @Kimberypalet Very sorry, it was a typo. See the edit to my answer. If this works for you please accept my answer by clicking the check mark (tick mark) to the left of the answer.

    – Mary
    Apr 11 at 16:01











  • Hi. Thank you again. Can you help me again ?? I run on another problem. What if the passing grade is only 1,2 and 3 and 5 is equals to Failed and there is also another "INC" means incomplete so i need to set the passing grade as string but Cint function is only for Integer ?

    – Kimberypalet
    Apr 12 at 20:10











  • I suggest you accept my answer and ask a new question.

    – Mary
    Apr 12 at 21:07











  • Sorry. Thank you so much.

    – Kimberypalet
    Apr 12 at 21:28















1














Comments in line.



Private conString As String = "Your connection string"

Private Sub EnrollStudent(StudentID As Integer, SubjectID As Integer)
'Question Does Subject have a prerequisite?
Dim Prerequisite As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select pre_id from subject_bsit
where subject_id = @subject_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
Prerequisite = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - No, It is OK to enroll student there are no prerequisites
If Prerequisite = 0 Then
InsertEnrollment(StudentID, SubjectID)
Return
End If
'Answer - Yes there Is a prerequisite
'New Question - Has the student passed the prerequisite?
Dim PassingGrade As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select Grade From student_subject
Where subject_id = @subject_id
And student_id = @student_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = Prerequisite
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cn.Open()
PassingGrade = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - Yes, student has a passing grade.
If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)
Else 'Answer - No, student does not have a passing grade in prerequisite.
MessageBox.Show("Student cannot enroll because of prerequisite.")
End If
End Sub

Private Sub InsertEnrollment(StudentID As Integer, SubjectID As Integer)
Dim query = "Insert Into student_subject (student_id, subjectId, Grade, Enrolled)
Values (@student_id, @subject_id, 0, 1);"
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand(query, cn)
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Successful enrollment")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EnrollStudent(1235, 5)
End Sub


EDIT



Typo here...



If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)


This should be



If PassingGrade > 0 Then
InsertEnrollment(StudentID, SubjectID)


The second argument is SubjectID






share|improve this answer

























  • Hi, it is working if i only try to add a subject with no prerequisite. but i try to add the ID of subject that has a prereisite this is the error. Cannot add or update a child row: a foreign key constraint fails (csais.student_subject, CONSTRAINT student_subject_ibfk_2 FOREIGN KEY (sub_id) REFERENCES subject_bsit (subject_id))

    – Kimberypalet
    Apr 11 at 4:45











  • @Kimberypalet Very sorry, it was a typo. See the edit to my answer. If this works for you please accept my answer by clicking the check mark (tick mark) to the left of the answer.

    – Mary
    Apr 11 at 16:01











  • Hi. Thank you again. Can you help me again ?? I run on another problem. What if the passing grade is only 1,2 and 3 and 5 is equals to Failed and there is also another "INC" means incomplete so i need to set the passing grade as string but Cint function is only for Integer ?

    – Kimberypalet
    Apr 12 at 20:10











  • I suggest you accept my answer and ask a new question.

    – Mary
    Apr 12 at 21:07











  • Sorry. Thank you so much.

    – Kimberypalet
    Apr 12 at 21:28













1












1








1







Comments in line.



Private conString As String = "Your connection string"

Private Sub EnrollStudent(StudentID As Integer, SubjectID As Integer)
'Question Does Subject have a prerequisite?
Dim Prerequisite As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select pre_id from subject_bsit
where subject_id = @subject_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
Prerequisite = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - No, It is OK to enroll student there are no prerequisites
If Prerequisite = 0 Then
InsertEnrollment(StudentID, SubjectID)
Return
End If
'Answer - Yes there Is a prerequisite
'New Question - Has the student passed the prerequisite?
Dim PassingGrade As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select Grade From student_subject
Where subject_id = @subject_id
And student_id = @student_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = Prerequisite
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cn.Open()
PassingGrade = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - Yes, student has a passing grade.
If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)
Else 'Answer - No, student does not have a passing grade in prerequisite.
MessageBox.Show("Student cannot enroll because of prerequisite.")
End If
End Sub

Private Sub InsertEnrollment(StudentID As Integer, SubjectID As Integer)
Dim query = "Insert Into student_subject (student_id, subjectId, Grade, Enrolled)
Values (@student_id, @subject_id, 0, 1);"
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand(query, cn)
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Successful enrollment")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EnrollStudent(1235, 5)
End Sub


EDIT



Typo here...



If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)


This should be



If PassingGrade > 0 Then
InsertEnrollment(StudentID, SubjectID)


The second argument is SubjectID






share|improve this answer















Comments in line.



Private conString As String = "Your connection string"

Private Sub EnrollStudent(StudentID As Integer, SubjectID As Integer)
'Question Does Subject have a prerequisite?
Dim Prerequisite As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select pre_id from subject_bsit
where subject_id = @subject_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
Prerequisite = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - No, It is OK to enroll student there are no prerequisites
If Prerequisite = 0 Then
InsertEnrollment(StudentID, SubjectID)
Return
End If
'Answer - Yes there Is a prerequisite
'New Question - Has the student passed the prerequisite?
Dim PassingGrade As Integer
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand("Select Grade From student_subject
Where subject_id = @subject_id
And student_id = @student_id", cn)
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = Prerequisite
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cn.Open()
PassingGrade = CInt(cmd.ExecuteScalar)
End Using
End Using
'Answer - Yes, student has a passing grade.
If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)
Else 'Answer - No, student does not have a passing grade in prerequisite.
MessageBox.Show("Student cannot enroll because of prerequisite.")
End If
End Sub

Private Sub InsertEnrollment(StudentID As Integer, SubjectID As Integer)
Dim query = "Insert Into student_subject (student_id, subjectId, Grade, Enrolled)
Values (@student_id, @subject_id, 0, 1);"
Using cn As New MySqlConnection(conString)
Using cmd As New MySqlCommand(query, cn)
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cmd.Parameters.Add("@subject_id", MySqlDbType.Int32).Value = SubjectID
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Successful enrollment")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EnrollStudent(1235, 5)
End Sub


EDIT



Typo here...



If PassingGrade > 0 Then
InsertEnrollment(StudentID, StudentID)


This should be



If PassingGrade > 0 Then
InsertEnrollment(StudentID, SubjectID)


The second argument is SubjectID







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 11 at 15:59

























answered Mar 24 at 5:52









MaryMary

4,4762921




4,4762921












  • Hi, it is working if i only try to add a subject with no prerequisite. but i try to add the ID of subject that has a prereisite this is the error. Cannot add or update a child row: a foreign key constraint fails (csais.student_subject, CONSTRAINT student_subject_ibfk_2 FOREIGN KEY (sub_id) REFERENCES subject_bsit (subject_id))

    – Kimberypalet
    Apr 11 at 4:45











  • @Kimberypalet Very sorry, it was a typo. See the edit to my answer. If this works for you please accept my answer by clicking the check mark (tick mark) to the left of the answer.

    – Mary
    Apr 11 at 16:01











  • Hi. Thank you again. Can you help me again ?? I run on another problem. What if the passing grade is only 1,2 and 3 and 5 is equals to Failed and there is also another "INC" means incomplete so i need to set the passing grade as string but Cint function is only for Integer ?

    – Kimberypalet
    Apr 12 at 20:10











  • I suggest you accept my answer and ask a new question.

    – Mary
    Apr 12 at 21:07











  • Sorry. Thank you so much.

    – Kimberypalet
    Apr 12 at 21:28

















  • Hi, it is working if i only try to add a subject with no prerequisite. but i try to add the ID of subject that has a prereisite this is the error. Cannot add or update a child row: a foreign key constraint fails (csais.student_subject, CONSTRAINT student_subject_ibfk_2 FOREIGN KEY (sub_id) REFERENCES subject_bsit (subject_id))

    – Kimberypalet
    Apr 11 at 4:45











  • @Kimberypalet Very sorry, it was a typo. See the edit to my answer. If this works for you please accept my answer by clicking the check mark (tick mark) to the left of the answer.

    – Mary
    Apr 11 at 16:01











  • Hi. Thank you again. Can you help me again ?? I run on another problem. What if the passing grade is only 1,2 and 3 and 5 is equals to Failed and there is also another "INC" means incomplete so i need to set the passing grade as string but Cint function is only for Integer ?

    – Kimberypalet
    Apr 12 at 20:10











  • I suggest you accept my answer and ask a new question.

    – Mary
    Apr 12 at 21:07











  • Sorry. Thank you so much.

    – Kimberypalet
    Apr 12 at 21:28
















Hi, it is working if i only try to add a subject with no prerequisite. but i try to add the ID of subject that has a prereisite this is the error. Cannot add or update a child row: a foreign key constraint fails (csais.student_subject, CONSTRAINT student_subject_ibfk_2 FOREIGN KEY (sub_id) REFERENCES subject_bsit (subject_id))

– Kimberypalet
Apr 11 at 4:45





Hi, it is working if i only try to add a subject with no prerequisite. but i try to add the ID of subject that has a prereisite this is the error. Cannot add or update a child row: a foreign key constraint fails (csais.student_subject, CONSTRAINT student_subject_ibfk_2 FOREIGN KEY (sub_id) REFERENCES subject_bsit (subject_id))

– Kimberypalet
Apr 11 at 4:45













@Kimberypalet Very sorry, it was a typo. See the edit to my answer. If this works for you please accept my answer by clicking the check mark (tick mark) to the left of the answer.

– Mary
Apr 11 at 16:01





@Kimberypalet Very sorry, it was a typo. See the edit to my answer. If this works for you please accept my answer by clicking the check mark (tick mark) to the left of the answer.

– Mary
Apr 11 at 16:01













Hi. Thank you again. Can you help me again ?? I run on another problem. What if the passing grade is only 1,2 and 3 and 5 is equals to Failed and there is also another "INC" means incomplete so i need to set the passing grade as string but Cint function is only for Integer ?

– Kimberypalet
Apr 12 at 20:10





Hi. Thank you again. Can you help me again ?? I run on another problem. What if the passing grade is only 1,2 and 3 and 5 is equals to Failed and there is also another "INC" means incomplete so i need to set the passing grade as string but Cint function is only for Integer ?

– Kimberypalet
Apr 12 at 20:10













I suggest you accept my answer and ask a new question.

– Mary
Apr 12 at 21:07





I suggest you accept my answer and ask a new question.

– Mary
Apr 12 at 21:07













Sorry. Thank you so much.

– Kimberypalet
Apr 12 at 21:28





Sorry. Thank you so much.

– Kimberypalet
Apr 12 at 21:28



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310588%2fsql-query-for-subject-prerequisite%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript