我的问题: 我有3个表,user,debit和user_list。我需要在一个查询中(因为我在android上使用了Cursor)并且最好不要使用rawQuery来获取user.name以及链接到该用户的所有借记的总和,状态= 1。
例如:
User Debit User_List
id | name user_id | value user_id | status
1 | John 1 | 10.23 1 1
2 | Marie 1 | 22 2 1
2 | 2333
当状态等于1时,它应返回类似下面的内容:
name value
John 32.23
Marie 2333
到目前为止我做了什么:
SELECT (select SUM(debit_value) from debits where debit_status = 0) AS user_balance, user_name AS user_name, status, users.user_id AS user_id, _id FROM users INNER JOIN users_listview ON users.user_id = users_listview.user_id WHERE (status = ?) GROUP BY users._user_id
谢谢!
答案 0 :(得分:0)
使用以下查询
select User.name, sum(Value) from User inner join Debit
on User.id=Debit.userid
Group by User.name
答案 1 :(得分:0)
试试这个,
SELECT U.NAME,SUM(COALESCE(D.VALUE,0)) AS TOTAL_VALUE FROM USER U LEFT OUTER JOIN DEBIT D ON U.ID=D.USER_ID GROUP BY U.NAME
答案 2 :(得分:0)
我的最后一个问题:
SELECT SUM(debit_value) AS user_balance, user_name AS user_name, status, users.user_id AS user_id, users.user_id AS _id FROM users INNER JOIN users_listview ON users.user_id = users_listview.user_id INNER JOIN debits ON users.user_id = debit_to WHERE (status = ?) GROUP BY debit_to
谢谢大家!