使用查询的结果进行进一步的计算,这些结果在同一查询中给出错误

发布于 2024-11-19 00:28:18 字数 3479 浏览 1 评论 0原文

我已经这样写了查询 我想用这些结果进一步计算,对于所有单个查询来说,

SELECT  TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
   DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments,
   30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount, 
   (totalcontractamountperiod/monthlyamount) as exppayments, 
   member_Id 
FROM membertomships;

是否所有的结果都可能使用 mysql 进行单个查询,

但它给出了这样的错误,

Error Code: 1054
Unknown column 'totalcontractamountperiod' in 'field list'

任何人都可以了解这个

修改的代码:我已经通过使用子查询完成了这样的操作....

SELECT  
  EXPPAYMENT WHERE EXPPAYMENT =(TIMESTAMPDIFF(MONTH, memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments , 
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount , 
  (totalcontractamountperiod/monthlyamount) as exppayments, member_Id 
FROM membertomships);

但它给出了这样的错误

Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE EXPPAYMENT =(TIMESTAMPDIFF(MONTH, memberToMship_StartDate,memberToMship_En' at line 1

,这是修改后的查询,并向查询添加了另外两个函数,但它仍然给出了

SELECT 
  Total,
  (datdiff-1) as diff,
  ceil(ExpPayments-Total) AS datdiff,                 
  ExpPayments, 
  MonthlyAmount, 
  (Total/MonthlyAmount) as ExpPayments2, 
  member_Id 
FROM 
( 
  SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*    memberToMship_ChargePerPeriod As Total,     
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
  member_Id
FROM 
  membertomships 
) as SourceTable;

这样的错误错误,

                     Error Code: 1054
                     Unknown column 'datdiff' in 'field list'

请帮忙

               SELECT 
                SourceTable.Total,
               ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
              ceil(SourceTable.ExpPayments-SourceTable.Total) -1 as diff,
                       ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(ceil(SourceTable.ExpPayments-   SourceTable.Total) -1) MONTH), INTERVAL DAY(NOW()) - SourceTable.memberToMship_DueDay DAY) As expdate,
              SourceTable.ExpPayments, 
             SourceTable.MonthlyAmount, 
           (SourceTable.Total/SourceTable.MonthlyAmount) as ExpPayments2, 
            SourceTable.member_Id 
        FROM 
        ( 
              SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*      memberToMship_ChargePerPeriod As Total,     
          DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
          30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
           member_Id
          FROM 
         membertomships 
      ) as SourceTable

错误代码:1054 “字段列表”中的未知列“SourceTable.memberToMship_DueDay”

i have written the query like this
and i want to caluculate further with these results that to all in single query

SELECT  TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
   DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments,
   30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount, 
   (totalcontractamountperiod/monthlyamount) as exppayments, 
   member_Id 
FROM membertomships;

is it possible that all comes in single query using mysql

but it was giving error like this

Error Code: 1054
Unknown column 'totalcontractamountperiod' in 'field list'

can anyone have idea about this

MODIFIED CODE : I have done like this by using subqueries....

SELECT  
  EXPPAYMENT WHERE EXPPAYMENT =(TIMESTAMPDIFF(MONTH, memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments , 
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount , 
  (totalcontractamountperiod/monthlyamount) as exppayments, member_Id 
FROM membertomships);

but it was giving error like this

Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE EXPPAYMENT =(TIMESTAMPDIFF(MONTH, memberToMship_StartDate,memberToMship_En' at line 1

and this is the modified query and added two more functions to the query but sill it was giving error

SELECT 
  Total,
  (datdiff-1) as diff,
  ceil(ExpPayments-Total) AS datdiff,                 
  ExpPayments, 
  MonthlyAmount, 
  (Total/MonthlyAmount) as ExpPayments2, 
  member_Id 
FROM 
( 
  SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*    memberToMship_ChargePerPeriod As Total,     
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
  member_Id
FROM 
  membertomships 
) as SourceTable;

error like this

                     Error Code: 1054
                     Unknown column 'datdiff' in 'field list'

would you pls help

               SELECT 
                SourceTable.Total,
               ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
              ceil(SourceTable.ExpPayments-SourceTable.Total) -1 as diff,
                       ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(ceil(SourceTable.ExpPayments-   SourceTable.Total) -1) MONTH), INTERVAL DAY(NOW()) - SourceTable.memberToMship_DueDay DAY) As expdate,
              SourceTable.ExpPayments, 
             SourceTable.MonthlyAmount, 
           (SourceTable.Total/SourceTable.MonthlyAmount) as ExpPayments2, 
            SourceTable.member_Id 
        FROM 
        ( 
              SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*      memberToMship_ChargePerPeriod As Total,     
          DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
          30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
           member_Id
          FROM 
         membertomships 
      ) as SourceTable

Error Code: 1054
Unknown column 'SourceTable.memberToMship_DueDay' in 'field list'

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

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

发布评论

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

评论(2

木森分化 2024-11-26 00:28:18

我将缩写一些列名称,因为它们有点长,因此查询可以更清晰:

SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
        DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
        30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
--->    (Total/MonthlyAmount) as ExpPayments2,     
        member_Id  
FROM membertomships; 

问题出在指出的行中,因为您正在使用 Total 和 < code>MonthlyAmount 在定义它们的同一列列表中。我知道它看起来应该可以工作,但是这样使用 SQL 是非法的。

您可以做的是:

  1. 重复定义值的条件(并希望数据库只计算一次),如下所示

SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
        DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
        30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
        (TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod / (30* ChargePerPeriod / DATEDIFF(EndDate,StartDate))) as ExpPayments2,     
        member_Id  
FROM membertomships; 
  1. 像子查询一样重新处理表达式。您只需一步即可完成计算,然后使用这些结果进行进一步计算,如下所示

SELECT Total, ExpPayments, MonthlyAmount, (Total/MonthlyAmount) as ExpPayments2, member_Id  from
(
  SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
          DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
          30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
          member_Id  
  FROM membertomships
) as SourceTable

顺便说一句,原始查询中的两列名为 ExpPayments,我将第二个列重命名为 ExpPayments2。

I'll abreviate some of the column names as they are a bit on the long side, so the query can be more clear:

SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
        DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
        30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
--->    (Total/MonthlyAmount) as ExpPayments2,     
        member_Id  
FROM membertomships; 

The problem is in the pointed row, as you are using the Total and MonthlyAmount in the same column list that defines them. I know it look like it should work, but it's illegal to use SQL like that.

What you could do is:

  1. Repeat the condition that defines the values (and hope that the database will only calculate it once), like

this:

SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
        DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
        30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
        (TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod / (30* ChargePerPeriod / DATEDIFF(EndDate,StartDate))) as ExpPayments2,     
        member_Id  
FROM membertomships; 
  1. Rework the expression like a subquery. You get the calculations done in one step, and than use those results for further calculations, like

this:

SELECT Total, ExpPayments, MonthlyAmount, (Total/MonthlyAmount) as ExpPayments2, member_Id  from
(
  SELECT  TIMESTAMPDIFF(MONTH,StartDate,EndDate)* ChargePerPeriod As Total,
          DATEDIFF(GREATEST(NOW(),DueDay),StartDate)/30 AS ExpPayments,    
          30* ChargePerPeriod / DATEDIFF(EndDate,StartDate) AS MonthlyAmount,
          member_Id  
  FROM membertomships
) as SourceTable

BTW, two columns in the original query are named ExpPayments, I renamed the second ExpPayments2.

长途伴 2024-11-26 00:28:18
SELECT 
  derived.totalcontractamountperiod,
  derived.exppayments,
  derived.monthlyamount,
  (derived.totalcontractamountperiod/derived.monthlyamount) as exppayments2,
  derived.member_Id
FROM
( 
  SELECT  
    TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
    DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments,
    30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount, 
    (totalcontractamountperiod/monthlyamount) as exppayments, 
    member_Id 
  FROM membertomships
) derived

修改后的代码也有同样的问题。应该是:

SELECT 
  SourceTable.Total,
  ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
  ceil(SourceTable.ExpPayments-SourceTable.Total) -1 as diff,
  SourceTable.ExpPayments, 
  SourceTable.MonthlyAmount, 
  (SourceTable.Total/SourceTable.MonthlyAmount) as ExpPayments2, 
  SourceTable.member_Id 
FROM 
( 
  SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*      memberToMship_ChargePerPeriod As Total,     
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
  member_Id
  FROM 
    membertomships 
) as SourceTable
SELECT 
  derived.totalcontractamountperiod,
  derived.exppayments,
  derived.monthlyamount,
  (derived.totalcontractamountperiod/derived.monthlyamount) as exppayments2,
  derived.member_Id
FROM
( 
  SELECT  
    TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)* memberToMship_ChargePerPeriod As totalcontractamountperiod ,
    DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS exppayments,
    30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS monthlyamount, 
    (totalcontractamountperiod/monthlyamount) as exppayments, 
    member_Id 
  FROM membertomships
) derived

You've got the same problem with the modified code. It should be:

SELECT 
  SourceTable.Total,
  ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,                 
  ceil(SourceTable.ExpPayments-SourceTable.Total) -1 as diff,
  SourceTable.ExpPayments, 
  SourceTable.MonthlyAmount, 
  (SourceTable.Total/SourceTable.MonthlyAmount) as ExpPayments2, 
  SourceTable.member_Id 
FROM 
( 
  SELECT TIMESTAMPDIFF(MONTH,memberToMship_StartDate,memberToMship_EndDate)*      memberToMship_ChargePerPeriod As Total,     
  DATEDIFF(GREATEST(NOW(),memberToMship_DueDay),memberToMship_StartDate)/30 AS ExpPayments,
  30* memberToMship_ChargePerPeriod / DATEDIFF(memberToMship_EndDate,memberToMship_StartDate) AS MonthlyAmount, 
  member_Id
  FROM 
    membertomships 
) as SourceTable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文