我有两个名为member
andexpense
的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
提前致谢。