无法从android中的sqlite查询中获得正确的结果

时间:2013-11-10 17:31:29

标签: android

我有两个名为memberexpense的表以及一个用于连接目的的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

这给了我membersexpense的正确统计数,但当我想将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之类的所有数字。提前谢谢。

2 个答案:

答案 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
`

希望得到这个帮助。