I’m trying to get a product search to work properly in MySQL 5.0.88.
Basic setup: I have table A with products
A.id
A.ean
A.styleNo
A.price
A.seller (ID e.g. 123, 456)
And a table B with pricelists
B.name
B.ean
B.alt_price
B.seller
Sellers can define optional pricelists, which are matched to the user doing the search. My search more or less looks like this:
SELECT A.styleNo, A.price, B.price
FROM arts A
LEFT JOIN pricelists B ON
B.seller = A.seller
AND B.ean = A.ean
AND B.alt_price != 0
WHERE...
// pricelists
AND ( B.name = "some_name" AND B.seller = "123" ) OR ( next applicable seller ) ...
So after the LEFT JOIN, I’m including all items from a pricelist by name and seller.
This works ok as it selects both the regular price (A) and alt_price (B) and I can check for existing alt_price when displaying the results to show the correct price to the user.
However if the seller does not give an alt-price to all of his products, I’m now displaying the product with the price from A, when in fact I DON’T want to display products from this seller which do not have a pricelist entry at all (think regional assortment).
So if user X has a pricelist “abc” and seller 123 has 500 products, 200 of which are on the pricelist “abc”, I only want to display the 200 products and not 500 with 200 in the correct price.
I tried to use B.alt_price != 0 in the LEFT JOIN, but this doesn’t help, because all items on there have a price.
Question
Is there a way to do this in the actual search or do I have to do it in the results loop, which I’m not really keen on doing.
Ok. Finally found the solution.
The problem is, the search will contain multiple sellers, some of whome use pricelists and some who don’t.
I solved it like this:
– I’m having to construct this line anyway:
So I created another variable, that includes all seller IDs who use pricelists and are applicable to this user. Looks like this:
123,456,789…
I add this to the WHERE clause:
AND ( IF( A.seller IN ( 123,456,789… ), B.alt_price IS NOT NULL,1 ) )
This way, I’m checking
(a) if the record is from a pricelist seller applicable to the user, and
(b) if that’s the case, the records must not have a NULL value in the b.alt_price, which records not being on the pricelist will have, since sql adds NULL to all records not on the pricelist B when LEFT JOINING.
That was difficult…