using subqueries in main query
I have done like subquery like this but it was giving error
SELECT
sourceTable3.Total,
(sourcetable3.datediff-1) as diff,
ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(diff) MONTH), INTERVAL DAY(NOW()) - memberToMship_DueDay DAY) As expdate,
ExpPayments,
MonthlyAmount,
SourceTable3.ExpPayments2,
FROM
(
SELECT
SourceTable2.Total,
(SourceTable2.datdiff-1) as diff,
SourceTable2.datdiff,
ExpPayments,
MonthlyAmount,
SourceTable2.ExpPayments2,
member_Id
FROM
(
SELECT
SourceTable.Total,
ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,
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
) as SourceTable2
) as sourceTable3
but it was giving error like this...
Error Code: 1054
Unknown column 'sourcetable3.datediff' in 'field list'
would any one pls help...
MODIFIED CODE :
modified 'e' and got the error like this
Error Code: 1054 Unknown column 'memberTo_Mship_DueDay' in 'field list'
MODIFIED CODE :
this is my table structure
membership table
memberToMship_Id
memberToMship_StartDate
memberToMship_EndDate
memberToMship_DueDay
member_Id
I want finally these columns expdate , diff , ExpPayments,
MonthlyAmount,
MODIFIED CODE :
SELECT
SourceTable3.Total,
ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(diff) MONTH), INTERVAL DAY(NOW()) - memberToMship_DueDay DAY) As expdate,
(Sourcetable3.datdiff-1) as diff,
ExpPayments,
MonthlyAmount,
SourceTable3.ExpPayments2,
SourceTable3.member_Id
FROM
(
SELECT
SourceTable2.Total,
(SourceTable2.datdiff-1) as diff,
SourceTable2.datdiff, 开发者_如何学编程
ExpPayments,
MonthlyAmount,
SourceTable2.ExpPayments2,
member_Id
FROM
(
SELECT
SourceTable.Total,
ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,
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
) as SourceTable2
) as SourceTable3
and error
Error Code: 1054
Unknown column 'memberToMship_DueDay' in 'field list'
would any one pls help...
I think that you need to remove the e
from datediff
so it becomes sourcetable3.datdiff
.
SELECT
SourceTable2.Total,
ADDDATE(ADDDATE(NOW(), INTERVAL FLOOR(diff) MONTH), INTERVAL DAY(NOW()) -memberToMship_DueDay DAY) As expdate,
datdiff,
diff,
SourceTable2.ExpPayments,
SourceTable2.MonthlyAmount,
(SourceTable2.Total/SourceTable2.MonthlyAmount) as ExpPayments2,
SourceTable2.expdate,
SourceTable2.member_Id
FROM
(
SELECT
memberToMship_DueDay,
SourceTable.Total,
SourceTable.expdate
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.expdate,
SourceTable.member_Id
FROM
(
SELECT
memberToMship_DueDay,
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
) as SourceTable2
You have used different cases for SourceTable3
:
sourceTable3.Total,
(sourcetable3.datediff-1) as diff,
SourceTable3.ExpPayments2,
) as sourceTable3
If you are on Linux, try changing them all to SourceTable3
. See Identifier Case Sensitivity.
Looks like you have a typo in the line where that column is identified:
ceil(SourceTable.ExpPayments-SourceTable.Total) AS datdiff,
-----------^^-----
But you should enclose datediff
in backquotes since it is a reserved MySQL keyword.
ceil(SourceTable.ExpPayments-SourceTable.Total) AS `datediff`,
Well, which part of this error message is giving you trouble? There is simply no column named datediff in sourcetable3, it is datdiff
精彩评论