Table IMAGES
:
columns- imageID, Image, and categoryID*(this being the foreign key)
Table CATEGORIES
:
columns categoryID, Category
and peform query for example:
$sql = "SELECT categ开发者_运维知识库oryID FROM IMAGES WHERE image = 'exampleImage'";
I would get the result as an integer of the category ID i.e 1
My question is what would be the query to display the category that the ID belongs to? Or any suggestions on how to work around with multiple queries.
You want to use an inner join so it returns the records that match based on the join criteria.
SELECT Images.CategoryID, Category.Category
FROM IMAGES
INNER JOIN Category ON Category.CategoryID = Images.CategoryID
WHERE image = exampleimage
use a simple left join:
SELECT imageID, Image, IMAGE.categoryID, CATEGORIES.Category
FROM IMAGES
LEFT JOIN CATEGORIES
ON IMAGES.categoryID = CATEGORIES.categoryID
WHERE image = 'exampleImage'
use table aliases to make the statement shorter:
SELECT imageID, Image, i.categoryID, c.Category
FROM IMAGES i
LEFT JOIN CATEGORIES c
ON i.categoryID = c.categoryID
WHERE image = 'exampleImage'
精彩评论