mysql - 返回不同列中具有特定值的所有行
问题描述
我有一张看起来像这样的桌子
Time Master_Price Discounted_price1 Discounted_qty1 Discounted_price2 Discounted_qty2 Discounted_price3 Discounted_qty3
1552279758 100 90 5 80 10 70 15
1552279759 200 195 6 185 7 80 12
1552279760 300 285 11 200 9 150 7
1552279761 400 300 20 250 25 220 30
1552279763 500 400 30 350 5 300 8
1552279766 500 400 NULL 350 9 300 9
Time 列是唯一的,采用 unix 格式。
要求是对 Master_Price 列进行算术运算,并检查哪个 Discounted Price 与之匹配并返回相应的 Discounted Qty。
假设如果 Master_Price 是 100 并且我输入 Master_Price - 20,那么应该返回值 12(存在于第二行)或整行。如果 Master_Price 为 200 并且我输入 Master_Price - 50,则应返回值 7(存在于第三行)或整行,依此类推。如果 Master_Price 是 500 并且我输入 Master_Price - 100 那么它应该返回 30 或整行而不是具有 NULL 的行
输入要从 Master_Price 中减去的整数的选项应该在查询中。即使是硬编码也没关系
解决方案
请参阅上面的评论,但我认为此查询应该有效:
(如果没有找到,它不会得到整行。)
select @MP := 500;
select @DISCOUNT := 100;
select
CASE
WHEN Discounted_price1 = Master_Price - @DISCOUNT THEN Discounted_qty1
WHEN Discounted_price2 = Master_Price - @DISCOUNT THEN Discounted_qty2
WHEN Discounted_price3 = Master_Price - @DISCOUNT THEN Discounted_qty3
ELSE 'None'
END as qty
from prices where Master_Price = @MP and ((Discounted_price1 = Master_Price - @DISCOUNT and Discounted_qty1 >=0) or (Discounted_price2 = Master_Price - @DISCOUNT and Discounted_qty2 >=0) or (Discounted_price3 = Master_Price - @DISCOUNT and Discounted_qty3 >=0));
我做了一个小提琴来演示/玩它。 http://sqlfiddle.com/#!9/0166cc/16
推荐阅读
- visual-studio - 如何在 VS 2015 中修复此错误消息“源不可用”
- amazon-cognito - AWS Cognito JS SDK 返回“AdminUpdateUserAttributes 不是函数”错误消息
- python - 使用 apply() 为 pandas 中的客户数据添加“价值分数”。在每一行之后,我无法将分数重置为零
- python - 执行命名实体识别 - NLP
- javascript - 无法通过 JQuery 在 JavaScript 中的 onChange 事件中获取元素 ID
- html - 从打字稿(Angular)修改html
- scala - 完全分割一块巧克力需要多少次休息?
- python - 如何为 serving_input_receiver_fn BERT Tensorflow 制作功能
- angular - 发送新数据时重新加载页面 Ionic 4
- java - 如何在 Spring Boot 中单独修改通道内每个接收器的每个代理消息?