Help with SQL script to see what is in your basket?
I have a MySQL shopping basket set up. There are 11 products in total running from ID 1 to 11. I want to make a query to see if they have any combination of 1, 2, 3 or 4 so that if they have more than one 1 or a 1 and a 2 I can apply a discount. If they have 1, 2开发者_开发知识库, 3 or 4 on their own I don't want to apply the discount.
The basket table is filtered on the session id. So here's what I have but is no good:
SELECT COUNT( * )
FROM `CycloneCentralBaskets`
WHERE `sessionID` = '174ca923b8a78fe5c2c2ef8322e180d2'
AND `p_id` =4
Any help as its boggling my mind?!
I think you're just needing to select where the p_id is in that range of values:
SELECT COUNT( * )
FROM `CycloneCentralBaskets`
WHERE `sessionID` = '174ca923b8a78fe5c2c2ef8322e180d2'
AND `p_id` IN ( 1, 2, 3, 4 );
That way, if they have two 1s, or a 1 and a 3, the overall count will be 2. If they only have one item on its own, they will have an overall count of 1.
This assumes you're only interested in knowing if a discount is eligible; are you needing to know which items are in the basket, or any other relevant info?
精彩评论