我有两个mysql表tblSharesRegistry,tblSharesAccount。我想连接两个表并按日期显示行。但它显示数据不正确。请帮忙。
1. tblSharesRegistry
ID EMPNumber NoOfShares Amount Deposit Date
1 1111 100 10000 1 2012-07-20
2 1234 50 5000 1 2012-08-12
3 1001 10 1000 1 2012-09-25
4 1001 10 1000 1 2012-10-25
5 1001 10 1000 1 2012-11-25
6 1001 -5 -500 0 2012-11-28
2. tblSharesAccount
ID EMPNumber Balance Interest Rate Date
1 1111 10000 1000 10 2012-11-29
2 1234 5000 500 10 2012-11-29
3 1001 2500 250 10 2012-11-29
输出应为EMPNumber:1001
Date Description #Shares Amount Interest Total
2012-09-25 Shares bought 10 1000 - 1000
2012-10-25 Shares bought 10 1000 - 2000
2012-11-25 Shares bought 10 1000 - 3000
2012-11-28 Shares sold -5 -500 - 2500
2012-01-29 Interest 25 2500 250 2750
我目前加入两个表的代码
SELECT tblSharesRegistry.Date, tblSharesRegistry.NoOfShares, tblSharesRegistry.Amount, tblSharesAccount.Interest, tblSharesAccount.Date FROM tblSharesAccount INNER JOIN tblSharesRegistry ON tblSharesAccount.EMPNumber = tblSharesRegistry.EMPNumber WHERE tblSharesRegistry.EMPNumber = '1001' ORDER BY Date DESC
VB.NET错误:
答案 0 :(得分:1)
试试这个:
SELECT * FROM
(SELECT sr.Date, IF(sr.NoOfShares > 0, 'Shares bought', 'Shares sold') Description,
sr.NoOfShares, sr.Amount, '-' Interest, sr.Amount Total
FROM tblSharesRegistry sr WHERE sr.EMPNumber = '1001'
UNION
SELECT sa.Date, 'Interest' Description, (SELECT SUM(sr.NoOfShares) FROM tblSharesRegistry sr WHERE sa.EMPNumber = se.EMPNumber) NoOfShares,
sa.Balance, sa.Interest, (sa.Balance + sa.Interest) Total
FROM tblSharesAccount sa WHERE sa.EMPNumber = '1001' ) AS A
ORDER BY A.date
检查此链接,以便更好地了解if else语句MySQL IF ELSE
答案 1 :(得分:0)
试试这个::
SELECT
tblSharesRegistry.Date,
tblSharesRegistry.NoOfShares,
tblSharesRegistry.Amount,
tblSharesAccount.Interest,
tblSharesAccount.Date
FROM tblSharesAccount
INNER JOIN tblSharesRegistry ON tblSharesAccount.EMPNumber = tblSharesRegistry.EMPNumber
WHERE tblSharesRegistry.EMPNumber = '1001' ORDER BY tblSharesRegistry.Date DESC