MYSQL 内连接错误 - 'm.account_no' 中的未知列 'where clause'

标签 mysql sql select join group-by

我不知道怎么调用它,我正在尝试解释它。

我有两个表:

<强>1。成员

enter image description here

<强>2。 share_trx_history

enter image description here

一个成员可以有多个分享记录,我必须在下面的结构中显示它 (给定年份的总借方、贷方、余额期初)

+-----------+------+-------+--------+---------+--------+
|account_no | name | debit | credit | balance | opening|
+-----------+------+-------+--------+---------+--------+

我已经试过了,但是失败了:

SELECT m.account_no, m.name, share.*
FROM `member` AS m
INNER JOIN (
    SELECT sth.account_no AS sth_account_no, SUM(sth.debit) AS sth_debit, SUM(sth.credit) AS sth_credit,(
    SELECT sth2.balance 
    FROM `share_trx_history` AS sth2 
    WHERE sth2.account_no=m.account_no
    ORDER BY sth2.share_issue_date ASC 
    LIMIT 0,1
) AS sth_balance, 
    (SELECT balance
        FROM `share_trx_history` AS sth3
        WHERE year(sth3.share_issue_date ) <2014 AND sth3.account_no=m.account_no
        ORDER BY sth3.share_issue_date DESC
        LIMIT 0 , 1) AS sth_opening 
FROM `share_trx_history` AS sth 
WHERE sth.share_issue_date >= DATE_SUB(NOW(), INTERVAL 1 Year)
AND sth.account_no=m.account_no) AS share
ON share.sth_account_no =  m.account_no

给出以下错误:

Unknown column 'm.account_no' in 'where clause'  

有什么简单的方法可以实现吗?

我的查询有什么问题?

谢谢

更新:

余额 =(总借方 + 总股息)-(总贷方)

请检查opening = previous year balance

最佳答案

试试这个:

SELECT m.account_no, m.name, SUM(sth.debit) AS sth_debit, SUM(sth.credit) AS sth_credit, 
       MAX(CASE share_issue_date WHEN start_date THEN balance ELSE 0 END) AS sth_balance, 
       MAX(CASE share_issue_date WHEN opening_date THEN balance ELSE 0 END) AS sth_opening
FROM `member` AS m 
INNER JOIN share_trx_history AS sth ON m.account_no = sth.account_no
INNER JOIN (SELECT account_no, MIN(share_issue_date) start_date, MAX(share_issue_date) opening_date
            FROM share_trx_history WHERE YEAR(share_issue_date) <2014 GROUP BY account_no
          ) AS A ON sth.account_no = A.account_no AND share_issue_date IN (start_date, opening_date)
WHERE sth.share_issue_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
GROUP BY m.account_no

关于MYSQL 内连接错误 - 'm.account_no' 中的未知列 'where clause',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21084381/

相关文章:

mysql - 报告所有可能的列组合

php - 选择除查询中两个表中匹配的行之外的所有行

MySQL 选择所有关系均为 x 的位置

MySQL 5.0 报告 "concat does not exist"

mysql - SELECT 和 WHERE 中的 SQL CASE 用于选择行

sql - 如何将sql语句存储在oracle表中?

php - 使用foreach从数据库中获取数据到数组中

Mysql 根据列名和字段值连接表

MySQL 其中 DateTime 大于今天

mysql - 我在 mysql 数据库中收到一个错误,说 "#1136 column count doesn' t 匹配第 1 行的值计数”