Click here to Skip to main content
15,883,936 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have 2 tables category , file
category table have id, bannerid, thumbid,
file table has id, path

i want like

select category.id, category.bannerid, category.thumbid ,file.path left join file on category.bannerid = file.id as bannerimagepath


select category.id, category.bannerid, category.thumbid ,file.path left join file on category.thumbid = file.id as thumbimagepath

What I have tried:

hi I have 2 tables file
file
id | Path
1 | c/image1.jpg
2 | c/image2.jpg
3 | c/image3.jpg
4 | c/image4.jpg
5 | c/image5.jpg
6 | c/image6.jpg
and second tabel
bannerimages
id | bigimageid | thumbimageid
1 | 2 | 1
2 | 3 | 4
3 | 5 | 6
I have to find out like
id | banner path | thumb path
1 | c/image2.jpg | c/image1.jpg
1 | c/image3.jpg | c/image4.jpg
1 | c/image5.jpg | c/image6.jpg
Posted
Updated 23-Jul-17 23:28pm
v2
Comments
BinnyVishwant 17-Jul-17 4:32am    
Could you please explain clearly! what exactly you need to do/required?
Ramesh Vishvas 17-Jul-17 6:29am    
can you describe more about your requirement?
Tomas Takac 17-Jul-17 6:45am    
You are missing the FROM clause. The query must look like this: SELECT ... FROM category LEFT JOIN file ON ...

You need to join to the file table twice. For that reason you need to assign the table two different aliases so there is no ambiguity when referencing it.
SQL
select
	category.id,
	bannerfile.[path] as bannerimagepath,
	thumbfile.[path] as thumbimagepath
from 
	[category]
	left join [file] as bannerfile on category.bannerid = bannerfile.id
	left join [file] as thumbfile on category.thumbid = thumbfile.id
 
Share this answer
 
Comments
Member 12530125 21-Jul-17 12:28pm    
thanks
You can use following query

Select id,(Select path from file where A.bigimageid =id ) as bannerPath,
(Select path from file where A.thumbimageid =id ) as ThumbPath FROM Bannerimages A
 
Share this answer
 
v3
Suppose if banner image table if somewhere in bannerid or thumbid contain null value

then how will write query

Can you tell me how to deal with null in join
 
Share this answer
 
Comments
Atlapure Ambrish 24-Jul-17 6:07am    
The LEFT join is made for that. In Thomas' solution he has already suggested to use LEFT join, this should solve your problem. What is the issue?

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