0

I want to show images from database.I have two tables gallery_photos

gallery_category enter image description here

I want to show any photos_filename with category_name. photo_category is the foreign key of category_id

mysql query i wrote

SELECT distinct a.category_name
       ,b.photo_filename 
FROM gallery_category a  
inner join gallery_photos b  on (a.category_id=b.photo_category)

please help me to do that...

i want to select exactly like this

albums/1456226111.jpg interior
albums/1456226239.jpg graphics
albums/1456226339.jpg random
albums/1456226478.jpg goods
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
Siby Xavier
  • 136
  • 1
  • 1
  • 18
  • albums/1456226122.jpg albums/1456226153.jpg albums/1456226168.jpg this images belogs from interior category. so why you write in graphics, random and goods ? – Vipin Jain Feb 24 '16 at 10:00

2 Answers2

2

you can grouping it by group by

SELECT a.category_name,b.photo_filename 
FROM gallery_category a  JOIN gallery_photos b  
ON a.category_id=b.photo_category 
GROUP BY a.category_name
Vipin Jain
  • 3,686
  • 16
  • 35
1

Use Left join to keep all the rows in the left table.

SELECT 
  b.photo_filename, 
  a.category_name 
FROM 
  gallery_category a  LEFT join gallery_photos b  ON (a.category_id=b.photo_category)

check this out!

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

Community
  • 1
  • 1
LKW
  • 180
  • 1
  • 3
  • 13
  • 1
    its show all image from all category but he want only one image per category am i right ? @SibyXavier – Vipin Jain Feb 24 '16 at 10:05
  • but what if there is more than one image in the category?what is the criteria to pick ? – LKW Feb 24 '16 at 10:08