Click here to Skip to main content
15,907,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to select records `product_id product_name actual_price discount_price` from `oc_product, oc_product_description, oc_product_special`. `oc_product_special` table contains discount price of product but on some products there is no discount i.e. discount_price=0.0000. I want to select all 40 products with their discount_price and if discount_price=0.0000 then print NULL in that row.

Query which I tried is:
SQL
`SELECT oc_product_description.product_id,oc_product_description.name product_name, oc_product.price actual_price, oc_product_special.price discount_price FROM oc_product INNER JOIN oc_product_description RIGHT OUTER JOIN oc_product_special ON oc_product_description.product_id = oc_product.product_id = oc_product_special.product_id`


it returns me wrong output like

`product_id 	product_name 	actual_price 	discount_price
  NULL 	          NULL 	           NULL 	       0.0000
  NULL            NULL 	           NULL 	       0.0000
  NULL 	          NULL 	           NULL 	       0.0000
  NULL 	          NULL 	           NULL 	       0.0000
  NULL 	          NULL 	           NULL 	       0.0000
  NULL 	          NULL 	           NULL 	       0.0000
  NULL 	          NULL 	           NULL 	       1950.0000
  NULL 	          NULL 	           NULL 	       3850.0000
  NULL 	          NULL 	           NULL 	       7500.0000`


And I expect this output:

`product_id 	product_name 	actual_price 	discount_price
  1               yyyy 	           1000.0000 	  0.0000
  2               xxxx 	           2000.0000 	  500.0000`
Posted
Updated 9-Aug-15 20:57pm
v3
Comments
Advay Pandya 10-Aug-15 1:53am    
Can you please put some clarity in your question ? What is wrong in your result and what are you trying to achieve with this query ?
Vilas Galave 10-Aug-15 2:21am    
@Advay Pandya - see my edited question

Seems your query is wrong.

It should be something like:
SQL
SELECT oc_product_description.product_id,oc_product_description.name product_name, oc_product.price actual_price, oc_product_special.price discount_price
FROM oc_product
    INNER JOIN oc_product_description ON oc_product_description.product_id = oc_product.product_id 
    RIGHT OUTER JOIN oc_product_special ON oc_product.product_id = oc_product_special.product_id



For further information, please see: 13.2.9.2 JOIN Syntax[^]

I'd suggest to use table aliases[^] to make query more clear.
 
Share this answer
 
Hello,

I agreed with the solution by [Maciej Los^].

In addition to the solution want to add one more thing.

As per my understanding of your query and requirement, "oc_product_special" table contains discount information.(If applicable). So, it should be Left outer join between oc_product and oc_product_special tables. Because in your query result of left side tables is more important. so, u will not get the product_Id with NULL values.

Please try below query:

SQL
SELECT oc_product_description.product_id,oc_product_description.name product_name, oc_product.price actual_price, oc_product_special.price discount_price
FROM oc_product
    INNER JOIN oc_product_description ON oc_product_description.product_id = oc_product.product_id
    LEFT OUTER JOIN oc_product_special ON oc_product.product_id = oc_product_special.product_id


Please let me know if you have any concern or query on this.

Thanks
 
Share this answer
 
Comments
Vilas Galave 11-Aug-15 0:22am    
@Advay Pandya - that's right! absolutely right.
Advay Pandya 11-Aug-15 1:00am    
Thanks... :)
Maciej Los 11-Aug-15 2:12am    
5ed!
Advay Pandya 11-Aug-15 3:06am    
Thanks Maciej.... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900