我有两个名为member
和expense
的表以及一个用于连接目的的bridge
表,我希望expense
应该平均分为成员是expense
UPDATE
//Create Member table
CREATE TABLE member(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);
// Create Expense table
CREATE TABLE expense (_id INTEGER PRIMARY KEY AUTOINCREMENT, amount INTEGER, expenseItemName TEXT);
// Create Bridge Table
CREATE TABLE bridge( bridgeId, _id INTEGER FOREGIN KEY REFERENCES member( _id ) ON DELETE CASCADE, exp_id INTEGER FOREGIN KEY REFERENCES expense(_id) ON DELETE CASCADE);
,我得到count of present members
这样的MEMBER COUNT QUERY
SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id
这给了我members
中expense
的正确统计数,但当我想将expense
分成这样的现有成员时
DIVIDE EXPENSE.AMOUNT QUERY
SELECT expense._id, itemName, bridge._id, expense.amount/(SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id), (SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id) AS total_member, bridge.bridge_id FROM member, expense, bridge WHERE (bridge._id = member._id AND member._id = 2) AND expense._id = bridge.exp_id
使用此查询我得到第一个expense.amount
的正确结果,但其他记录不正确,例如,如果在MEMBER COUNT QUERY
我们得到3,5
的结果,那么在DIVIDE EXPENSE.AMOUNT QUERY
在这种情况下,expense.amount
只会被first
数字划分为3
。在这种情况下,如何修改DIVIDE EXPENSE.AMOUNT QUERY
以将expense.amount
除以3,5
之类的所有数字。提前谢谢。
答案 0 :(得分:1)
尝试:
SELECT expense._id, itemName, bridge._id, expense.amount/
(SELECT count(*)
FROM expense as E2, bridge as B2
WHERE E2._id = B2.exp_id
AND B2._id = M._id),
(SELECT count(*)
FROM expense as E2, bridge as B2
WHERE E2._id = B2.exp_id
AND B2._id = M._id) AS total_member,
bridge.bridge_id
FROM member as M, expense as E, bridge as B
WHERE (B._id = M._id AND M._id = 2)
AND E._id = B.exp_id
更新1:
想一想,我不确定sqlite是否支持这种子查询。试试这个:
SELECT E._id, itemName, B._id, E.amount/table_count.count,
table_count.count AS total_member,
B.bridge_id
FROM member as M, expense as E, bridge as B,
(SELECT B2._id as b2_id, count(*) as count
FROM expense as E2, bridge as B2
WHERE E2._id = B2.exp_id
GROUP BY B2._id) as table_count
WHERE (B._id = M._id AND M._id = 2)
AND E._id = B.exp_id
AND table_count.b2_id = m._id
更新2:
再次尝试,这次我创建了一个测试模式,所以也许您可以使用我在下面给出的解决方案。希望这次有用:
SELECT E._id, expenseItemName, B._id, E.amount,
table_count.count AS total_member,
E.amount/table_count.count
FROM member as M, expense as E, bridge as B,
(SELECT B2.exp_id as b2_exp_id, count(*) as count
FROM expense as E2, bridge as B2
WHERE E2._id = B2.exp_id
GROUP BY B2.exp_id) as table_count
WHERE (B._id = M._id AND M._id = 2)
AND E._id = B.exp_id
AND table_count.b2_exp_id = E._id
您可以在此处尝试:http://www.sqlfiddle.com/#!7/aeca2/13但您需要Chrome或SQLite。我修改了一下你的架构,但我希望你能得到图片,然后根据你的情况做一些小改动。
答案 1 :(得分:0)
1.0的倍数expense.amount
可能会有效。这是sql
`SELECT expense._id, itemName, bridge._id, expense.amount * 1.0 /(SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id), (SELECT count(*) FROM member, expense, bridge WHERE expense._id = bridge.exp_id AND member._id = bridge._id GROUP BY expense._id) AS total_member, bridge.bridge_id FROM member, expense, bridge WHERE (bridge._id = member._id AND member._id = 2) AND expense._id = bridge.exp_id
`
希望得到这个帮助。