Lets say Template and category both have primary keys called ID and TempXcat has columns called TemplateID and CategoryID
SELECT template.* FROM template LEFT JOIN TempXCat on template.id=TempXCat.Templ
SELECT category.* FROM category LEFT JOIN TempXCat on category.id=TempXCat.categ
Or
SELECT Template.* from template where template.id not in (select templateID from TempXCat)
SELECT * from category where id not in (select categoryID from TempXCat)
Regards
Tushar
Main Topics
Browse All Topics





by: ee_rleePosted on 2008-04-20 at 08:26:53ID: 21396382
hi, you can do it in at least 3 ways
1. Using left join (I prefer this one) . When the ID in templates does not match in tempXcat, all values on tempXcat will return NULL.
SELECT t.*
FROM templates t LEFT JOIN
tempXcat x ON t.TemplateID=x.TemplateID
WHERE x.TemplateID IS NULL
2. Using not exists. Checks if an ID in templates does not exist in tempXcat.
SELECT *
FROM templates t
WHERE NOT EXISTS (SELECT 1 FROM tempXcat WHERE TemplateID=t.TemplateID)
3. Using not in. The subquery returns a list of IDs from tempXcat and the not in statement returns records from templates not in the list.
SELECT *
FROM templates t
WHERE TemplateID NOT IN (SELECT DISTINCT TemplateID FROM tempXcat)
> I need the same thing for all categories not represented in the crossover table.
Just change templates to categories and their corresponding id/linked fields.