MySQL 查询选择和计算值

发布于 2024-12-10 13:00:58 字数 479 浏览 1 评论 0原文

SELECT 
if('1000'>plan_limit_max,'1000'-plan_limit_max,0) as 'Overage',
if(`per_hour_cost`='0', `per_period_cost`+'10'*`Overage`, per_hour_cost*720) AS 'Total'

FROM `service_price`

order by Total asc
;

第 3 行出现 Overage 错误 有

Error Code: 1054. Unknown column 'Overage' in 'field list'

没有办法通过一个查询来完成我想要的操作? 我想按总价对结果进行排序,但要计算它,我必须计算超出限制的使用情况。

我知道应该有其他方法可以做到这一点,使用多个查询,创建视图等。 我只是想知道是否有更简单的查询。

SELECT 
if('1000'>plan_limit_max,'1000'-plan_limit_max,0) as 'Overage',
if(`per_hour_cost`='0', `per_period_cost`+'10'*`Overage`, per_hour_cost*720) AS 'Total'

FROM `service_price`

order by Total asc
;

There is an error with Overage at line 3

Error Code: 1054. Unknown column 'Overage' in 'field list'

Is there a way to do what I want with one query?
I want to order results by total price, but to calculate it I have to calculate the usage exceeds the limit.

I know there should be other ways to do it, use more than one queries, create views, etc..
I just wonder if there is a simpler query for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒尘 2024-12-17 13:00:58

您可以在子查询中计算一次Overage

SELECT Overage,
       IF(per_hour_cost = 0, per_period_cost + 10 * Overage, per_hour_cost * 720)
         AS Total
  FROM (SELECT IF(1000 > plan_limit_max, 1000 - plan_limit_max, 0) AS Overage,
               service_price.*
          FROM service_price) d;

You can compute Overage once, in a subquery:

SELECT Overage,
       IF(per_hour_cost = 0, per_period_cost + 10 * Overage, per_hour_cost * 720)
         AS Total
  FROM (SELECT IF(1000 > plan_limit_max, 1000 - plan_limit_max, 0) AS Overage,
               service_price.*
          FROM service_price) d;
山有枢 2024-12-17 13:00:58

Overage 未定义为查询的一部分,仅定义为结果中的列标题。您无法引用该列,因为它还不存在...您必须使用类似于下面的 SQL 的内容:

SELECT 
if('1000'>plan_limit_max,'1000'-plan_limit_max,0) as 'Overage',
if(`per_hour_cost`='0', `per_period_cost`+'10'*if('1000'>plan_limit_max,'1000'-             plan_limit_max,0), 
per_hour_cost*720) AS 'Total'

FROM `service_price`

order by Total asc

Overage is not defined as part of the query, only as the column header in the result. You cannot reference that column as it does not yet exist... You've got to use something like the SQL below:

SELECT 
if('1000'>plan_limit_max,'1000'-plan_limit_max,0) as 'Overage',
if(`per_hour_cost`='0', `per_period_cost`+'10'*if('1000'>plan_limit_max,'1000'-             plan_limit_max,0), 
per_hour_cost*720) AS 'Total'

FROM `service_price`

order by Total asc
冬天旳寂寞 2024-12-17 13:00:58

您在 OVERAGE 中输入“``”时出错。您必须在 OVERAGE 中使用“”。

SELECT 
if('1000'>plan_limit_max,'1000'-plan_limit_max,0) as 'Overage',
if(`per_hour_cost`='0', `per_period_cost`+'10'* 'Overage', per_hour_cost*720) AS 'Total'

FROM `service_price`

order by Total asc
;

You have an error putting " `` " in OVERAGE. You must use ' ' in OVERAGE.

SELECT 
if('1000'>plan_limit_max,'1000'-plan_limit_max,0) as 'Overage',
if(`per_hour_cost`='0', `per_period_cost`+'10'* 'Overage', per_hour_cost*720) AS 'Total'

FROM `service_price`

order by Total asc
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文