Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i display all columns from one table & only one column from the other table using joins?
for example:
i have 2 tables
Table A
column name:
pk (integer)
description (varchar)
type (varchar)

Table B
Column name:
pk (integer)
descr (varchar)
type (varchar)
fk (int) "this fk is related to Table A's pk"

How can i get the output as:
pk--> from table A
description--> from table A
type--> from table A
fk--> from table B
Posted

Try:
SQL
SELECT 
  tableA.PK, tableA.Description, tableA.type, tableB.pk
FROm
  tableA
INNER JOIN tableB ON tableA.PK = tableB.FK
 
Share this answer
 
Comments
Nischal Bhatt 16-Feb-11 6:02am    
I used this query in MySQL & it works like a charm.
Thanks Sandeep appreciate it.
I only speak T-SQL, but I'd write it as:

SQL
select TableA.*, TableB.fk from TableA
inner join TableB on TableA.pk = TableB.fk
 
Share this answer
 
You canuse something like:

SQL
select a.pk, a.description, a.type, b.fk from table_a as a
inner join table_b as b on b.pk = a.pk;

 
Share this answer
 
Comments
Om Prakash Pant 16-Feb-11 6:02am    
for more examples on join, please check the following link:
http://www.plus2net.com/sql_tutorial/sql_inner_join.php
Nischal Bhatt 16-Feb-11 6:14am    
Thank you for sharing your knowledge Sir Appreciate it, & i'll keep the link in my bookmarks for reference.
I really love left or right join because these are really faster than inner joins. Answering your question by following your requirements EXACTLY:

1. Displaying all columns from one table and one column from other table:
--B.pk in case that you want the 'pk' column from B table:
SELECT A.*, B.pk FROM A LEFT JOIN B ON A.pk=B.fk; 


2. pk-> from table A, description-> from table A, type-> from table A, fk-> from table B:
SELECT A.pk, A.description, A.type, B.fk FROM A LEFT JOIN B ON A.pk=B.fk;


Greetings :cool:
 
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