Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frnds

I have 2 tables in mysql i want to select only 2 columns from table1 and want to join on table2 i had tried inner join,left join and all but its not working plz help

> select year,batch from tb1 left join tb2 on tb1;
Posted
Updated 23-Nov-13 7:13am
v2

1 solution

Your on is completely wrong.

You will need a key on one table and a foreign key column on the other that refers back to the first one.

The select then becomes something like
SQL
select year, batch
from tb1
left join tb2 on tb1.key = tb2.foreignkey

There is a tutorial on how to write MySQL queries at tutorialspoint[^]

[Edit]
If you have many columns that you want to match on between the tables then just add those columns to the ON clause. Eg
left join tb2 on tb1.key = tb2.foreignkey and tb1.col2 = tb2.col2 and tb1.col3 = tb2.col3 etc
 
Share this answer
 
v3
Comments
Arun-23 24-Nov-13 8:18am    
its not working it has pbm in clause my pbm is i have 2 tables in tb1 it has many columns on tat i want to select year,batch and want to join on tb2 in both the table has rollno. year and batch should match according to the rollno in tb2 like already in tb1
CHill60 24-Nov-13 9:10am    
See my edited solution

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