All of these queries work for me (granted I ran them on SQL Server, but there isn't any specific SQL Server syntax that wouldn't work for MySQL too)
Code:
SELECT DISTINCT s.SubCategoryId, s.Name FROM #SubCategories AS s
RIGHT JOIN #Products AS p ON s.SubCategoryId = p.SubCategoryId
Code:
SELECT s.SubCategoryId, s.Name FROM #SubCategories AS s
RIGHT JOIN #Products AS p ON s.SubCategoryId = p.SubCategoryId
GROUP BY s.SubCategoryId, s.Name
Code:
SELECT s.SubCategoryId, s.Name FROM #SubCategories AS s
INNER JOIN #Products AS p ON s.SubCategoryId = p.SubCategoryId
GROUP BY s.SubCategoryId, s.Name
My Test Data (this is SQL Server specific)
Code:
CREATE TABLE #SubCategories
(
SubCategoryId INT,
Name VARCHAR(25)
)
INSERT INTO #SubCategories VALUES (1, 'Shoes')
INSERT INTO #SubCategories VALUES (2, 'Boxes')
INSERT INTO #SubCategories VALUES (3, 'Shirts')
INSERT INTO #SubCategories VALUES (4, 'Games')
CREATE TABLE #Products
(
ProductId INT,
SubCategoryId INT
)
INSERT INTO #Products VALUES (1, 1)
INSERT INTO #Products VALUES (2, 1) // create a second product with the same sub-category
// omit sub-category 2
INSERT INTO #Products VALUES (3, 3)
INSERT INTO #Products VALUES (4, 4)
Results
Code:
ID Name
----------------
1 Shoes
3 Shirts
4 Games
Edit:
@
vinpkl ; your PHP code is incorrect, check your mysql_query statement, you have $result being passed into the function and you need to pass $qry
Bookmarks