English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have the following tables:

categories (category_ID, category)
stories_category (story_ID, category_ID)

I want to return all (category) from categories which do NOT have (category_ID) in stories_category for a given (story_ID)...

Any help? :) I was up til 6am trying to think this one through...

2007-05-13 03:02:06 · 5 answers · asked by Xarra 1 in Computers & Internet Programming & Design

5 answers

It's hard to test without an example database but wouldn't the following do the trick?

SELECT category FROM categories WHERE category_ID NOT IN (SELECT category_ID FROM stories_category WHERE story_ID=given_story_ID);


####
NOTE that you asked for all (category) from categories (i.e., only one row); the next two answers return EVERYTHING from categories (i.e., all rows). Hence, these answers are technically incorrect. :-)
####

2007-05-13 03:11:20 · answer #1 · answered by Anonymous · 1 0

SELECT * FROM categories
WHERE category_ID NOT IN (SELECT category_id from stories_category WHERE story_ID = given_id)

2007-05-13 10:11:33 · answer #2 · answered by Buzzy Buddy 3 · 0 1

This will return any unused categoryID's. (Those CatID's not used in the table Story.

SELECT Category.CatId, Category.Category
FROM Category
LEFT JOIN Story ON Category.CatId = Story.CatD
WHERE (((T_Story.CatID) Is Null));

2007-05-13 11:39:06 · answer #3 · answered by MarkG 7 · 0 0

This syntax should work for all versions of SQL, including MySQL versions prior to 4.2 (which don't support subqueries or inner joins; all the previous suggestions use subqueries). Just change VALUE to be the story_id key you want.

SELECT c.category
FROM categories c
LEFT JOIN stories_categories s
ON s.category_id = c.category_id
WHERE s.story_id = 'VALUE'
AND s.category_id IS NOT NULL

2007-05-13 11:19:18 · answer #4 · answered by Anonymous · 0 1

hmmm... this looks like a homework question, but here goes

select * from categories c
where c.category_ID not in (
select s.category_ID from stories_category s
where story_ID='a given story'
);

untested.

2007-05-13 10:14:04 · answer #5 · answered by Aaron W 3 · 0 2

fedest.com, questions and answers