I have a website where I want to create option for user to remove items from their bag. I am doing this task using many different queries.
Is there way to reduce the number of queries?
I have the following 3 tables:
Here is how I am doing it:
1- Get the
bag_ID of user who is using the site. This is by
select Bag_ID FROM [BAG_TB] b WHERE Account_ID = @Account_ID;
2- Based on above step, it checks how many records are on
BAG_ITEM_TB. This will tell me how many items are in current user bag.
Select Bag_ID FROM [BAG_ITEM_TB] WHERE [Bag_ID] = @Bag_ID;
3a- If there is 1 record returned from step #2, this means I have to delete bag. So do a delete on
3b- If there is more than 1 record returned from step #2, then only delete from
BAG_ITEM_TB because there is more than 1 item in bag... so do not delete from
Below one example of my delete queries:
delete FROM [BAG_ITEM_TB] WHERE [Bag_ID] = @Bag_ID AND [Product_ID] = @Prduct_ID; delete FROM [BAG_TB] WHERE [Bag_ID] = [Bag_ID]; delete from [IS_ACCOUNT_TB] WHERE Account_ID = @Account_ID;