【问题标题】:Is there a way to 'interpoloate' a column's value as the comparison operator in the WHERE clause in MySQL?有没有办法在 MySQL 的 WHERE 子句中将列的值“插入”作为比较运算符?
【发布时间】:2018-05-15 18:38:32
【问题描述】:

假设我有一个名为“comparison_operator”的列,其值如“>”、“=”、“

SELECT products.name FROM products WHERE products.price #{comparison_operator} products.cost

我相信这个问题的答案是否定的,但我想看看是否有其他方法可以达到同样的目的。谢谢!

【问题讨论】:

  • MySql 有“eval”函数吗?
  • 或多或少是的 @theGleep MySQL 有 PREPARE/EXECUTE 子句以允许动态 SQL 执行

标签: mysql where string-interpolation comparison-operators


【解决方案1】:

是的,但不是那样的。

CASE
  WHEN products.comparison_operator = ">"
    THEN products.price > products.cost
  WHEN products.comparison_operator = "<"
    THEN products.price < products.cost
  ELSE
    products.price = products.cost
END

【讨论】:

  • 是的,我也考虑过逐个比较比较操作符,但这似乎很麻烦。我想没有办法更动态地做到这一点?
  • 你可以写一个UDF把它放在一个地方而不是到处写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 2012-11-10
  • 2016-03-09
相关资源
最近更新 更多