Reputation: 1679
I have a modal pop up that currently holds checkboxes in it for an admin to be able to add features to a product. I needed to change this to be able to get tied to the category of the product on the current page, but my sql statement isn't working.
The tables here are Feature, Category and Marketing. Feature connects to Category with the CategoryID(named CategoryID in Feature & in Category tables), and Feature connects to Marketing with the FeatureID(called MarketingData in the Marketing table)
The MarketingTypeID of 3 tells the Marketing table to look for a Feature and will jump over to the feature table. The select statement worked before I added the Category table stuff to it, so obviously I coded that part wrong. Can someone help me get the select statement working?
This works:
SELECT DISTINCT FeatureID, FeatureTitle
FROM Feature
WHERE FeatureID NOT IN
(SELECT m.MarketingData FROM Marketing
WHERE MarketingTypeID = 3 AND ProductID = @ProductID)
ORDER BY FeatureTitle
This doesn't:
SELECT DISTINCT f.FeatureID, f.FeatureTitle FROM Feature f
INNER JOIN Category c
ON c.CategoryID = f.CategoryID
WHERE f.CategoryID = @CategoryID
AND f.FeatureID NOT IN
(SELECT m.MarketingData FROM Marketing m
WHERE m.MarketingTypeID = 3 AND m.ProductID = @ProductID)
ORDER BY f.FeatureTitle
EX: FeatureID #23, Web Tutorials, has a CategoryID of 32....this Category is called Platforms and is located in the Category table. FeatureID #23 is NOT in the Marketing table, therefore is NOT associated to the Product that was chosen, which is ProductID #1. I need a checkbox that says Web Tutorials to show up in the modal. Like I said before, it was just fine before I added all the stuff about the Category that is associated to the product that was selected by the user.
UPDATE: I have no idea why I was trying to write that statement the way I had it. I realized that it was much easier than I had originally thought and changed the statement. This works now, thank you for all the help everyone! I changed the SELECT statement to:
"SELECT DISTINCT f.FeatureID, f.FeatureTitle
FROM Feature f
LEFT JOIN Category c ON c.CategoryID = f.CategoryID
WHERE f.CategoryID IN
(SELECT CategoryID FROM CategoryLink
WHERE ProductID = @ProductID)
AND f.FeatureID NOT IN
(SELECT m.MarketingData FROM Marketing m WHERE m.MarketingTypeID = 3
AND m.ProductID = @ProductID)
ORDER BY f.FeatureTitle"
Upvotes: 2
Views: 181
Reputation: 50970
This is my second answer to this question, pursuing a different problem with the query.
Is the MarketingData field NULLable? If so, and if your sub-query returns and NULL values, your statement may not evaluate as you expect depending on certain database settings.
In theory, this won't make a difference between the two queries because they both contain the same sub-query, but if you happened to test with different @ProductID you may only have come across the problem with a particular product.
Upvotes: 0
Reputation: 50970
If you compare this JOIN
:
FROM Feature f INNER JOIN Category c ON c.CategoryID = f.CategoryID
with the database structure you'll see that you're trying to use a column (Feature.CategoryID
) that does not exist in your database. In fact, with the existing design you're going to have to write a pretty involved query to get from Feature
to Category
.
It may be true, however that there is a more direct relationship between Categories and the Features they can have that is not yet represented in your database. If that's true, you'll need to add a table CategoryFeatureLink
, then use that table along with Category
and Feature
in a three-way JOIN
.
Upvotes: 4