我有3张表,我想(CREATE VIEW)从table2和table 3中选择数据,然后根据存款和发出支票的月份和年份对这些数据进行分组。但是我没有达到预期的结果。
这是我的代码
1. tbl_deposits
+---------+------------------+--------+----------+
| transId | date(YYYY-MM-DD) | bankId | amount |
+---------+------------------+--------+----------+
| 1 | 2019-04-12 | 1 | 100.00 |
| 2 | 2019-04-11 | 1 | 200.00 |
| 3 | 2019-05-07 | 3 | 500.00 |
| 4 | 2019-05-11 | 3 | 300.00 |
| 5 | 2019-05-13 | 1 | 1,000.00 |
+---------+------------------+--------+----------+
2. tbl_bankAccount
+--------+----------+
| bankId | bankName |
+--------+----------+
| 1 | Bank 1 |
| 2 | Bank 2 |
| 3 | Bank 3 |
+--------+----------+
3. tbl_chequeIssue
+------------+-------------+--------+--------+
| checkIdNum | date_issued | bankId | amount |
+------------+-------------+--------+--------+
| 1 | 2019-05-08 | 1 | 50.00 |
| 2 | 2019-05-14 | 3 | 20.00 |
| 3 | 2019-05-19 | 3 | 10.00 |
| 4 | 2019-06-01 | 1 | 15.00 |
+------------+-------------+--------+--------+
我尝试了这段代码,但是没有用
SELECT DATE_FORMAT(a.Date, '%b , %Y') AS Date_Deposit,b.BankName AS
Bank_Name, a.Amount AS Deposited_Amount,
DATE_FORMAT(a.trans_time, '%b , %Y') AS Check_Issued,b.BankName, c.Amount
AS Amount_Issued
FROM tbl_deposits a
JOIN tbl_bankaccount b
ON a.bankaccount_id = b.bankaccount_id
JOIN tbl_checkissue c
ON a.bankaccount_id = c.bankaccount_id
GROUP BY
MONTH(c.trans_time),YEAR(c.trans_time),
MONTH(a.Date),YEAR(a.date),b.BankName
ORDER BY a.Date ASC,b.BankName
预期结果
+--------------+----------+------------+--------------+----------+---------------+
| Date_Deposit | BankName | Dep_Amount | Check_Issued | BankName | Amount_issued |
+--------------+----------+------------+--------------+----------+---------------+
| Apr, 2019 | BANK 1 | 300.00 | | | |
| May, 2019 | BANK 1 | 1,000.00 | May, 2019 | BANK 1 | 50.00 |
| May, 2019 | BANK 3 | 800.00 | May, 2019 | BANK 3 | 30.00 |
| | | | June, 2019 | BANK 1 | 15.00 |
+--------------+----------+------------+--------------+----------+---------------+
基于我的mysql代码的实际结果
+--------------+----------+------------+--------------+----------+---------------+
| Date_Deposit | BankName | Dep_Amount | Check_Issued | BankName | Amount_issued |
+--------------+----------+------------+--------------+----------+---------------+
| Apr, 2019 | BANK 1 | 100.00 | May, 2019 | BANK 1 | 50.00 |
| Apr, 2019 | BANK 1 | 100.00 | May, 2019 | BANK 1 | 15.00 |
| May, 2019 | BANK 3 | 500.00 | May, 2019 | BANK 3 | 20.00 |
| May, 2019 | BANK 1 | 1,000.00 | May, 2019 | BANK 1 | 50.00 |
| May, 2019 | BANK 1 | 1,000.00 | May, 2019 | BANK 1 | 15.00 |
+--------------+----------+------------+--------------+----------+---------------+
答案 0 :(得分:0)
尝试一下:
SELECT DATE_FORMAT(a.date, '%b , %Y') Date_Deposit,
c.BankName,SUM(a.amount) AS Dep_amount,
Check_issued,c.BankName,SUM(b.amount) AS Amount_issued
FROM tbl_deposits a
LEFT JOIN --one is doing left joining
(SELECT date_issued,
DATE_FORMAT(date_issued, '%b , %Y') Check_issued,
bankid,
SUM(amount) AS amount
FROM tbl_chequeissue
GROUP BY bankid, DATE_FORMAT(date_issued, '%b , %Y')) b
ON a.bankId=b.bankId
AND MONTH(a.date)=MONTH(date_issued)
AND YEAR(a.date)=YEAR(date_issued)
LEFT JOIN tbl_bankaccount c ON a.bankid=c.bankid
GROUP BY a.bankid, DATE_FORMAT(a.date, '%b , %Y')
UNION --union these two query
SELECT DATE_FORMAT(a.date, '%b , %Y') Date_Deposit,
c.BankName,SUM(a.amount) AS Dep_amount,
Check_issued,c.BankName,SUM(b.amount) AS Amount_issued
FROM tbl_deposits a
RIGHT JOIN --another is doing right joining
(SELECT date_issued,
DATE_FORMAT(date_issued, '%b , %Y') Check_issued,
bankid,
SUM(amount) AS amount
FROM tbl_chequeissue
GROUP BY bankid, DATE_FORMAT(date_issued, '%b , %Y')) b
ON a.bankId=b.bankId
AND MONTH(a.date)=MONTH(date_issued)
AND YEAR(a.date)=YEAR(date_issued)
LEFT JOIN tbl_bankaccount c ON a.bankid=c.bankid
GROUP BY a.bankid, DATE_FORMAT(a.date, '%b , %Y');
对两个类似的查询执行UNION
,仅用JOIN
运算符进行区分。