1

这是 MySQL 表的一部分

 Amount | DebitAccount | RecordYear | RecordMonth | RecordDay

在 MySQL 查询中需要RecordYear,RecordMonth,RecordDay更改为DATE.

根据许多建议获得这样的工作代码(我仅将代码作为示例显示它是如何工作的)

SELECT * FROM 
(SELECT * 

, CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate 


FROM 2_1_journal) DATA 
WHERE RecordDate <> ? AND DocumentDate = ?
ORDER BY RecordDate DESC

此代码从表中选择所有内容,将分隔的整数更改/转换为RecordDate并提供基于WHERE.

这是第二个工作代码也仅作为示例

SELECT a.AccountNumber, 
IFNULL( d.Amount, 0 ) - IFNULL( c.Amount, 0 ) AS Amount 
FROM 18_7_ChartOfAccounts AS a 
LEFT JOIN ( 
SELECT DebitAccount, 
SUM( Amount ) AS Amount FROM 2_1_journal WHERE 

CAST(RecordDay AS UNSIGNED) != ? AND CAST(RecordMonth AS UNSIGNED) != ? AND CAST(RecordYear AS UNSIGNED) != ? 

GROUP BY DebitAccount 
) d ON (a.AccountNumber = d.DebitAccount) 

需要这CAST(RecordDay AS UNSIGNED) != ? AND CAST(RecordMonth AS UNSIGNED) != ? AND CAST(RecordYear AS UNSIGNED) != ?部分的第二个代码

更改为, CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate第一个代码的这一部分。

我创建了这样的代码

SELECT a.AccountNumber, 
IFNULL( d.Amount, 0 ) - IFNULL( c.Amount, 0 ) AS Amount 
FROM 18_7_ChartOfAccounts AS a 
LEFT JOIN (
SELECT DebitAccount, 
SUM( Amount ) AS Amount , 

CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate 

FROM 2_1_journal 
WHERE RecordDate = ? 
GROUP BY DebitAccount 
) d ON (a.AccountNumber = d.DebitAccount) 


/*below code is for purpose to show whole code*/
LEFT JOIN ( SELECT CreditAccount, SUM( Amount ) AS Amount , CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate FROM 2_1_journal WHERE RecordDate = ? GROUP BY CreditAccount ) c ON (a.AccountNumber = c.CreditAccount)

在输出中得到这个错误SQLSTATE[42S22]: Column not found: 1054 Unknown column 'RecordDate' in 'where clause'

据了解CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate,必须位于其他地方。但是什么地方?可能需要DATA像第一个示例一样添加。

代码可能看起来难以理解,但这是获得必要内容的代码。请指教。

更新

根据建议更改代码

SELECT
a.AccountNumber, 
IFNULL( d.Amount, 0 ) - IFNULL( c.Amount, 0 ) AS Amount

FROM 18_7_ChartOfAccounts AS a

LEFT JOIN (
SELECT * FROM (
SELECT DebitAccount, 
SUM( Amount ) AS Amount , 

CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate,
CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate 

FROM 2_1_journal) DATA

WHERE RecordDate = 2013-01-20

GROUP BY DebitAccount 

) d ON (a.AccountNumber = d.DebitAccount)

我知道表格中有日期为 2013-01-20 的行。但输出结果为 0 且没有错误。

可能是我在代码中的疏忽错误....

得出的结论是代码仅在 MySQL 表的第一行检查日期。如果第一行中的日期与用户输入匹配,则代码将列中的所有值相加Amount。如果不匹配,则结果为 0。

更新 1

似乎终于这是工作代码。目前输出符合预期。如果可能,请提出改进​​建议

SELECT
a.AccountNumber, 
IFNULL( d.Amount, 0 ) - IFNULL( c.Amount, 0 ) AS Amount

FROM 18_7_ChartOfAccounts AS a

LEFT JOIN (
SELECT DebitAccount, 
SUM( Amount ) AS Amount FROM
( SELECT *  , CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate FROM 2_1_journal) DATA
WHERE

RecordDate = ?

GROUP BY DebitAccount 

) d ON (a.AccountNumber = d.DebitAccount)

LEFT JOIN (
SELECT CreditAccount, 
SUM( Amount ) AS Amount FROM
( SELECT * , CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate FROM 2_1_journal) DATA
WHERE

RecordDate = ?

GROUP BY CreditAccount 

) c ON (a.AccountNumber = c.CreditAccount)
4

1 回答 1

3

您不能只使用CAST,您还缺少SELECT第一个示例中的嵌套。没有它,您将无法在WHERE;中使用计算字段。

SELECT DebitAccount, 
SUM( Amount ) AS Amount , 
CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate , 
CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate 
FROM 2_1_journal 
WHERE RecordDate = ? 
GROUP BY DebitAccount 

...需要是...

SELECT * FROM (
  SELECT DebitAccount, 
  SUM( Amount ) AS Amount , 
  CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate,
  CAST(CONCAT_WS('-', DocumentYear,DocumentMonth,DocumentDay) AS DATE) DocumentDate 
  FROM 2_1_journal) DATA
WHERE RecordDate = ?
GROUP BY DebitAccount 

也就是说,此查询的性能不容易维护,您正在对计算字段进行操作,每次执行查询时都WHERE需要计算表中所有行的值。在表中有一个实际DATE的字段会加快速度。

于 2013-07-05T06:58:57.360 回答