Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
VB
      Table_1

Table_2    Table_3


am having three tables (table_1 , table_2 and table_3)

1) table_1 having Foreign key for table_2 and table_3
2) table_2 and table_3 have no relationship

My question is ,

Is possible to join table_2 and table_3 with the table_1 ?



Am doing project in MVC4, trying to implement select statement.

Is there any sample project related to this kindly share it for Reference
Posted
Comments
King Fisher 11-Jun-15 2:40am    
How do you join the two table without any relationship?

try to join table1 =table2 and table1=table3.but i'm not sure about your expected Resultsets.If you share your table structure with some dummy records ,then we can try it out.

Table1 having two foreign keys cust_id and branch_id:
SQL
create table loan_details(branch_id int(5), cust_id int(5), amount int(10), foreign key(branch_id) references branch_details(branch_id), foreign key(cust_id) references cust_details(cust_id));


Table2 having primary key cust_id:
SQL
create table cust_details(cust_id int(5), cust_name varchar(25),primary key(cust_id));


Table3 having primary key branch_id:
SQL
create table branch_details(branch_id int(5), branch_name varchar(25),primary key(branch_id));


Selection from 3 tables using natural join:
SQL
SELECT cust_id,cust_name,amount FROM cust_details c JOIN loan_details l ON c.cust_id=l.cust_id JOIN branch_details b ON l.branch_id=b.branch_id;
 
Share this answer
 
Table 2 and table 3 are having many-to-many relationship, for example:
1. a teacher (table 1) may teach more than one class (table 2) and a class may be taught by more than one teacher.
2. a customer (table 1) may buy any number of products (table 2) and a product may be bought by any number of customers.
To link these 2 tables, you need a third table (table 1) to pull them together. This table will have at least 2 fields, one each linking to the primary keys of the respective tables. One example:
Table Name      Columns
tbl_customer    cust_id (primary key)
                cust_name 
tbl_product
                product_id (primary key)
                product_name
                price
tbl_order
                order_id (primary key)
                cust_id (foreign key to tbl_customer)
                product_id (foreign key to tbl_product)

To find a particular order detail, you have to JOIN them together:
select order_id, cust_name, product_name, price from tbl_customer c join tbl_order o on c.cust_id = o.cust_id join tbl_product p on p.product_id = o.product_id where order_id=2

Reference:
1. http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html[^]
2. Visual Representation of SQL Joins[^]
 
Share this answer
 
v2
key's don't actually matter one jot for joins, they are used for enforcing constraints i.e. if you delete a customer then the constraint can delete all the orders that contain that customer_id.

can join anything to anything though, just learn the differences between inner and outer joins.
 
Share this answer
 

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