Jump to content

SELECT issue


rseigel
Go to solution Solved by rseigel,

Recommended Posts

I have the following SELECT that almost works:

SELECT product.reference AS sku, product.price - specific_price.reduction AS price, stock_available.quantity, marketplace_product_option.asin1 AS 'product-id', 'ASIN' AS 'product-id-type', product.condition AS 'condition-type'
FROM product, specific_price, stock_available, marketplace_product_option
WHERE product.id_product = specific_price.id_product
AND product.id_product = stock_available.id_product
AND product.id_product = marketplace_product_option.id_product

The problem is that sometimes there is no entry of specific_price.reduction (it's not ZERO - it just doesn't have a row in the table at all for that particular product).

 

What I want to do is if specific_price.reduction exists then do the math (product.price - specific_price.reduction)  otherwise just use product.price as the value.

 

I'd like to do this INLINE in the SELECT rather than resorting to PHP.

 

I hope that makes sense.

 

Ron

Link to comment
Share on other sites

you would need to explicitly use a LEFT JOIN between the other tables and the specific_price table, so that there will be a row in the result set even when there isn't one in the specific_price table. then use IFNULL(expr1,expr2) in the select term to get either the product.price - specific_price.reduction or just the product.price (expressions using a NULL value return a NULL.)

Link to comment
Share on other sites

ok.....sorted the alias names part....

SELECT p.reference AS sku, p.price - sp.reduction AS price, sa.quantity, m.asin1 AS 'product-id', 'ASIN' AS 'product-id-type', p.condition AS 'condition-type'
FROM product AS p, specific_price AS sp, stock_available AS sa, marketplace_product_option AS m
WHERE p.id_product = sp.id_product
AND p.id_product = sa.id_product
AND p.id_product = m.id_product

Still confused ablut the left join. :(

 

Do I need 3 left joins in the SELECT?

Edited by rseigel
Link to comment
Share on other sites

should (untested) work -

SELECT p.reference AS sku, IFNULL(p.price - sp.reduction,p.price) AS price, sa.quantity,
 mpo.asin1 AS 'product-id', 'ASIN' AS 'product-id-type', p.condition AS 'condition-type'
 FROM product p
 LEFT JOIN specific_price sp USING (id_product)
 JOIN stock_available sa USING (id_product)
 JOIN marketplace_product_option mpo USING (id_product)
Link to comment
Share on other sites

  • Solution

Thanks for the kick in the right direction.

 

I actually ended up using:

SELECT p.reference AS sku, IFNULL(p.price - sp.reduction,p.price) AS price, sa.quantity, mpo.asin1 AS 'product-id', 'ASIN' AS 'product-id-type', p.condition AS 'condition-type'
 FROM product p
 LEFT JOIN specific_price sp USING (id_product)
 JOIN stock_available sa USING (id_product)
 JOIN marketplace_product_option mpo USING (id_product)
 WHERE mpo.asin1 > ''

which actually eliminates NULL and blank values. :)

 

Off to the next challenge.....

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.