How to use case statements in mysql using joins?












0















I am working on product and category and product discount thing. I have done for a product discount. But the problem in while calculating category_discount. Here is my code :



SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount = 0 THEN products.product_price
WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
END) AS amount
FROM products
join
categories
on products.category_id = categories.id


See my result below it's not calculating category_discount for the first row:-
enter image description here










share|improve this question




















  • 1





    No images please. Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 21 '18 at 7:42











  • Case expression (since it returns a value.)

    – jarlh
    Nov 21 '18 at 8:22
















0















I am working on product and category and product discount thing. I have done for a product discount. But the problem in while calculating category_discount. Here is my code :



SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount = 0 THEN products.product_price
WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
END) AS amount
FROM products
join
categories
on products.category_id = categories.id


See my result below it's not calculating category_discount for the first row:-
enter image description here










share|improve this question




















  • 1





    No images please. Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 21 '18 at 7:42











  • Case expression (since it returns a value.)

    – jarlh
    Nov 21 '18 at 8:22














0












0








0








I am working on product and category and product discount thing. I have done for a product discount. But the problem in while calculating category_discount. Here is my code :



SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount = 0 THEN products.product_price
WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
END) AS amount
FROM products
join
categories
on products.category_id = categories.id


See my result below it's not calculating category_discount for the first row:-
enter image description here










share|improve this question
















I am working on product and category and product discount thing. I have done for a product discount. But the problem in while calculating category_discount. Here is my code :



SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount = 0 THEN products.product_price
WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
END) AS amount
FROM products
join
categories
on products.category_id = categories.id


See my result below it's not calculating category_discount for the first row:-
enter image description here







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 7:47









Eren ÇELİK

10617




10617










asked Nov 21 '18 at 7:41









kunalkunal

12010




12010








  • 1





    No images please. Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 21 '18 at 7:42











  • Case expression (since it returns a value.)

    – jarlh
    Nov 21 '18 at 8:22














  • 1





    No images please. Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 21 '18 at 7:42











  • Case expression (since it returns a value.)

    – jarlh
    Nov 21 '18 at 8:22








1




1





No images please. Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Madhur Bhaiya
Nov 21 '18 at 7:42





No images please. Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Madhur Bhaiya
Nov 21 '18 at 7:42













Case expression (since it returns a value.)

– jarlh
Nov 21 '18 at 8:22





Case expression (since it returns a value.)

– jarlh
Nov 21 '18 at 8:22












1 Answer
1






active

oldest

votes


















1














Then When-statements are evaluated in order, until one is fulfilled.
In first row, the first When-clause is fulfilled, so there is no discount. Try below instead:



 WHEN products.product_discount = 0  AND categories.category_discount = 0   THEN products.product_price


Or even reorder like this:



 SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
ELSE products.product_price
END) AS amount
...





share|improve this answer
























  • Nice! Please mark as accepted if it answered your question fully.

    – SAS
    Nov 21 '18 at 7:58













  • yeah sure its shwoing you can accept answer in 10 min

    – kunal
    Nov 21 '18 at 7:59











  • Oh, sorry, thanks! Happy to help.

    – SAS
    Nov 21 '18 at 7:59













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407313%2fhow-to-use-case-statements-in-mysql-using-joins%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Then When-statements are evaluated in order, until one is fulfilled.
In first row, the first When-clause is fulfilled, so there is no discount. Try below instead:



 WHEN products.product_discount = 0  AND categories.category_discount = 0   THEN products.product_price


Or even reorder like this:



 SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
ELSE products.product_price
END) AS amount
...





share|improve this answer
























  • Nice! Please mark as accepted if it answered your question fully.

    – SAS
    Nov 21 '18 at 7:58













  • yeah sure its shwoing you can accept answer in 10 min

    – kunal
    Nov 21 '18 at 7:59











  • Oh, sorry, thanks! Happy to help.

    – SAS
    Nov 21 '18 at 7:59


















1














Then When-statements are evaluated in order, until one is fulfilled.
In first row, the first When-clause is fulfilled, so there is no discount. Try below instead:



 WHEN products.product_discount = 0  AND categories.category_discount = 0   THEN products.product_price


Or even reorder like this:



 SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
ELSE products.product_price
END) AS amount
...





share|improve this answer
























  • Nice! Please mark as accepted if it answered your question fully.

    – SAS
    Nov 21 '18 at 7:58













  • yeah sure its shwoing you can accept answer in 10 min

    – kunal
    Nov 21 '18 at 7:59











  • Oh, sorry, thanks! Happy to help.

    – SAS
    Nov 21 '18 at 7:59
















1












1








1







Then When-statements are evaluated in order, until one is fulfilled.
In first row, the first When-clause is fulfilled, so there is no discount. Try below instead:



 WHEN products.product_discount = 0  AND categories.category_discount = 0   THEN products.product_price


Or even reorder like this:



 SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
ELSE products.product_price
END) AS amount
...





share|improve this answer













Then When-statements are evaluated in order, until one is fulfilled.
In first row, the first When-clause is fulfilled, so there is no discount. Try below instead:



 WHEN products.product_discount = 0  AND categories.category_discount = 0   THEN products.product_price


Or even reorder like this:



 SELECT products.id, products.product_name, products.product_price, products.product_discount, categories.category_discount,
(CASE WHEN products.product_discount != 0 THEN products.product_price - ( products.product_price * products.product_discount)/100
WHEN categories.category_discount !=0 THEN products.product_price - ( products.product_price * categories.category_discount)/100
ELSE products.product_price
END) AS amount
...






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 7:52









SASSAS

2,5041637




2,5041637













  • Nice! Please mark as accepted if it answered your question fully.

    – SAS
    Nov 21 '18 at 7:58













  • yeah sure its shwoing you can accept answer in 10 min

    – kunal
    Nov 21 '18 at 7:59











  • Oh, sorry, thanks! Happy to help.

    – SAS
    Nov 21 '18 at 7:59





















  • Nice! Please mark as accepted if it answered your question fully.

    – SAS
    Nov 21 '18 at 7:58













  • yeah sure its shwoing you can accept answer in 10 min

    – kunal
    Nov 21 '18 at 7:59











  • Oh, sorry, thanks! Happy to help.

    – SAS
    Nov 21 '18 at 7:59



















Nice! Please mark as accepted if it answered your question fully.

– SAS
Nov 21 '18 at 7:58







Nice! Please mark as accepted if it answered your question fully.

– SAS
Nov 21 '18 at 7:58















yeah sure its shwoing you can accept answer in 10 min

– kunal
Nov 21 '18 at 7:59





yeah sure its shwoing you can accept answer in 10 min

– kunal
Nov 21 '18 at 7:59













Oh, sorry, thanks! Happy to help.

– SAS
Nov 21 '18 at 7:59







Oh, sorry, thanks! Happy to help.

– SAS
Nov 21 '18 at 7:59






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407313%2fhow-to-use-case-statements-in-mysql-using-joins%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?