【问题标题】:Subquery returns more than 1 row - even when I use IN clause子查询返回超过 1 行 - 即使我使用 IN 子句
【发布时间】:2017-01-11 16:05:19
【问题描述】:

我需要创建一个小的 MySQL 触发器来计算价格税。我已经查看并搜索了答案,并尝试了很多关于ININNER JOIN 等的建议。但我仍然遇到这个错误,我不知道如何解决它。在表products 中有很多产品的价格,在表tax 中只有一行带有税值。我需要每个产品的价格除以这个税值。非常感谢您的帮助。

Select  ((Select    product_price_tax
          From      products
          Where     product_price_tax In (Select    product_price_tax
                                          From      products)
         ) / (Select    tax_value
              From      tax
             ))

【问题讨论】:

  • 如果有多个 product_price_tax 和/或 tax_value 怎么办?
  • 返回多个结果的子查询很可能是Select tax_value From tax 查询。
  • productstax 有什么关系?
  • 但是这部分Where product_price_tax In (Select product_price_tax From products)没有意义
  • 是的,product_price_tax 更多 -> 每个产品的价格只有一个 tax_value -> 所有产品的税值相同

标签: mysql sql triggers subquery


【解决方案1】:

如果没有错,你正在寻找这个

SELECT product_price_tax / IFNULL((SELECT tax_value
                                    FROM   tax), 0)
FROM   products 

IFNULL 用于在tax 值为0 的情况下处理除以零错误

【讨论】:

  • 是的,非常感谢您的帮助...我花了一整天的时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多