Jump to content

sql syntax help


samoht

Recommended Posts

SELECT i.ItemId, i.Name, f.Name
From item i, features f, itemfeatures itf
Where i.ItemId = itf.ItemId
AND f.FeatureId = itf.FeatureId 
AND f.Name = 'Sugar Free'
OR f.Name = 'Chocolate'

change to

SELECT i.ItemId, i.Name, f.Name
From item i, features f, itemfeatures itf
Where i.ItemId = itf.ItemId
AND f.FeatureId = itf.FeatureId 
AND (f.Name = 'Sugar Free' OR f.Name = 'Chocolate')

Link to comment
Share on other sites

Thanks for the help.

 

I still don't think this is exactly what I need because this returns all the records for both field names - but I only want the records that have both field names. The current db has only 41 'Sugar Free' items - so my query should return less than that.

Link to comment
Share on other sites

No, I want only those items that are both sugar free and chocolate.

 

I do not want all the sugar free and all the chocolate items. You see I have a top level nav that shows "Chocolate" | "Hard Candy | etc

under these I have a sub level nav that contains several options - one of which is "Sugar Free". So there is Sugar Free Chocolate and there is Sugar Free Hard Candy and so on. When I pick Sugar Free under the Chocolate top nav I only want sugar free chocolate items (not sugar free what ever - nor chocolate what ever)

 

Hopefully that makes sense

 

Link to comment
Share on other sites

After fooling around a lot and asking every one and getting all kinds of answers -

I may have a solution:

 

<?php
SELECT i.Name AS ItemName, pd.ItemId as Id, pp.ProductId, pd.Name, pd.PriceRetail, pp.PriceSell, i.ImagetnURL, QtyInStock, pack.PackageType, s.Name as Size
FROM product pd, item i, productprice pp, packaging pack, size s, brands b
WHERE i.ItemId IN 
(
SELECT ItemId 
FROM features f
INNER JOIN itemfeatures itf ON f.FeatureId = itf.FeatureId 
WHERE f.Name = 'Sugar Free'
)
AND i.ItemId IN 
(
SELECT ItemId 
FROM features f
INNER JOIN itemfeatures itf ON f.FeatureId = itf.FeatureId
WHERE itf.FeatureId = 'Chocolate'
) 
AND pd.PackageId = pack.PackageId
AND i.BrandId = b.BrandId
AND pd.SizeId = s.SizeId
AND pd.ItemId = i.ItemId
AND pd.ProductId = pp.ProductId
AND ClientPriceCode = 'R1'";

At least it seems to be on the right track - and maybe now I can sleep at night :)

 

does this make sense?

Link to comment
Share on other sites

ooppss,

 

No you were right FeatureId is an int not text

it should be:

WHERE i.ItemId IN 
(
SELECT ItemId 
FROM features f
INNER JOIN itemfeatures itf ON f.FeatureId = itf.FeatureId 
WHERE f.Name = 'Sugar Free'
)
AND i.ItemId IN 
(
SELECT ItemId 
FROM features f
INNER JOIN itemfeatures itf ON f.FeatureId = itf.FeatureId
WHERE f.Name = 'Chocolate'
)

Link to comment
Share on other sites

So does it work?

 

I just don't understand one thing; How is 'Sugar Free' and 'Chocolate' in the same field if one is a sub-category of the other? Just to be clear, they are in the same field? If this were the case, no row would be able to be 'Chocolate' and 'Sugar Free' at the same time. There has got to be something I am missing. Do you think you could post an example row with the field names of an entry that you want it to return?

Link to comment
Share on other sites

yes it can be a little confusing when you think of it from the stand point of categories.

 

My boss wanted to be able to store items in multiple categories - but also have a way of organizing the categories. So our db has categorygroups and categories - Chocolate is a categorygroup to which sugar free is a category.

 

The way we decided to best enable us to navigate through the db was by feature. - so we created a features table and an itemfeatures table  - each categorygroup and category has a matching feature - and any item can have multiple features - so that an Item (say Almond Joy) can have the features of Chocolate, Candy Bar, Almond, Coconut etc

 

an example output with out the feature name:

ItemNameItemIdProductIdProductNamePriceQtyInStock

Sugar Free Chocolate Bridge Mix1535 9217  Sugar Free Chocolate Bridge Mix 7.209999

Chocolate Pretzels Mini 1155 10120 Chocolate Pretzels Mini - Sugar Free - 5 Oz. Bags 1.16 9999

 

with the feature name:

ItemNameItemIdProductIdProductNamePriceQtyInStockfeature

Sugar Free Chocolate Bridge Mix1535 9217  Sugar Free Chocolate Bridge Mix 7.209999Chocolate

Sugar Free Chocolate Bridge Mix1535 9217  Sugar Free Chocolate Bridge Mix 7.209999Sugar Free

Chocolate Pretzels Mini 1155 10120 Chocolate Pretzels Mini - Sugar Free - 5 Oz. Bags 1.16 9999Chocolate

Chocolate Pretzels Mini 1155 10120 Chocolate Pretzels Mini - Sugar Free - 5 Oz. Bags 1.16 9999Sugar Free
Link to comment
Share on other sites

Oh. You are looking for a sub string inside of a field. The = operator compares the whole thing. You need to use LIKE.

"WHERE f.name LIKE '%Sugar Free%' AND f.name LIKE '%Chocolate%'"

 

That should return results where the field has both of thos substrings in it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.