using the results from query doing further calculations with these results in the same query giving error
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'
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:
- 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;
- 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.
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
精彩评论