Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i hv 3 tables..

1.tbl_course
SQL
id name
-------
1 | MCA
2 | MBA
3 | BSc
4 | MSC


2.tbl_branch
SQL
branch_id | course_id | branch_name
------------------------------------
1         |   2         |HR
2         |   2         |FINANCE
3         |   2         |MARKTING
4         |   3         |COMPUTER SCNCE
5         |   4         |ENGLISH


3.table_subjects
SQL
subject_id|course_id|branch_id|subject_name
-------------------------------------------
1         |   2     | 1       |SUB1
2         |   2     | 2       |SUB2
3         |   3     | 4       |SUB3

the table_subject fields are based on foriegnkey concept
how can i add values of table_course value MCA to table subject.
it doesnot have a branch so branch id is null.

but also i need a result like

SQL
course_name|Branch_name|subject_name
-------------------------------------------------
 MCA       |           |
 MBA       |     HR    |   SUB1 
 MBA       |   FINANCE |   SUB2


how can i achieve this... any idea???
Posted
Updated 17-Jun-13 4:10am
v2
Comments
Prasad Khandekar 17-Jun-13 10:03am    
If you have set up foreign keys on the subject table then you can not insert a row with foreign key column as null value. For this to work. Don't setup foreign key relation with other tables on table_subjects. You will then have to programmatically enforce that.

Any way the query to retrieve the desired result will be

SELECT c.name, b.name, s.name
FROM subject s
LEFT JOIN tbl_course c ON c.id = s.course_id
LEFT JOIN tbl_branch b ON b.branch_id = s.branch_id
CHill60 17-Jun-13 18:03pm    
Next time post the code that created the "syntax error" of the title, as well as full details of the error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900