Jump to content

Recommended Posts

I have a table called ads and a second table called images that has a list of images for each as by adID  I want to do a query for each and and get the first image(imageID) from the images table.  If there insn't a image then would be null.  How can I make a query like this?  I tried a subquery but didn't work for me.

 

--Ads--

AdID

AdName

 

--Images--

ImageID

AdID

ImageName

Link to comment
https://forums.phpfreaks.com/topic/63074-solved-sub-query/
Share on other sites

this calls for a JOIN i believe:

 

SELECT DISTINCT Ads.AdID, Ads.AdName, Images.ImageID, Images.ImageName FROM Ads INNER JOIN Images ON Ads.AdID=Images.AdID

 

give that a whirl - if syntax is off, check the manual and you should be able to correct it.

Link to comment
https://forums.phpfreaks.com/topic/63074-solved-sub-query/#findComment-314126
Share on other sites

I've tried that and it gives me duplicate rows if there are more than one image.  I need to get all rows in the Ad table and if there is an image get the first occurrence.  See below

 

--Sql--

 

SELECT DISTINCT Ads.AdID, Ads.CatID, images.Image

FROM

Ads

Left Join images ON Ads.AdID = images.AdID

 

--results--

AdID    CatID  Image

338 40_5 null

337 10_99 null

336 30 null

335 30 null

334 20 null

333 30 TestImage.jpg

333 30 TestImage2.jpg

333 30 TestImage3.jpg

332 30 null

Link to comment
https://forums.phpfreaks.com/topic/63074-solved-sub-query/#findComment-314219
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.