Relational Database simple search string
Hey guys, This is a follow-up to a question that I asked earlier. It is my first time using a relational 开发者_JS百科database and I need help with a quick search string to bring up desired results.
Background information: I'm making a database for my photo portfolio and want to be able to retrieve image links/data via their categories. Each image can be listed in multiple categories.
My database is set-up as follows :
TABLE tbl_images
(image_id
, image_title
, image_location
, image_descrip
,image_url
)
TABLE tbl_categories
(category_id
,category_name
,category_descrip
)
TABLE tbl_image_categories
(image_id
,category_id
)
Where one of my images (image_id=1) has two categories (Desert [category_id=1] and Winter [category_id=2]). Which I defined in tbl_image_categories as 1,1 and 1,2.
I also have a few other images that I defined as Desert images [category_id=1].
How would I go about getting which images should be loaded based on the Desert Category?
I tried:
SELECT tbl_images.image_url
FROM tbl_images,
tbl_image_categories,
tbl_categories
WHERE tbl_categories.category_id = 1
Try this:
SELECT DISTINCT tbl_images.image_url
FROM tbl_images,
tbl_image_categories,
tbl_categories
WHERE chad_categories.category_id = 1 //category_id=1 for Desert
AND chad_images.image_id = chad_image_categories.image_id
AND chad_image_categories.category_id = chad_categories.category_id
精彩评论