特殊选择语句(sqlite)

发布于 2024-10-07 13:39:51 字数 288 浏览 1 评论 0原文

  1. 表:“用户”

- 用户 ID - 姓名 -

(每个用户 ID 都是唯一的)

  1. 表:“金钱支出”

- 用户 ID - 金钱支出 -

(一个用户 ID 可能有多个具有不同“金钱支出”的条目)

现在,我需要的总和用户花的钱。

总而言之,我需要以下视图:


- 名称 - 总和(花费的钱) -

哪个语句可以给我这个结果?

  1. Table: "user"

- Userid - Name -

(every userid is unique)

  1. Table: "money spend"

- Userid - money spend -

(a userid may have several entries with different "money spend")

Now, I need the total sum of the money spend by a user.

To conclude, I need the following view:


- name - sum (money spend) -

Wich statement may give me this result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉月流沐 2024-10-14 13:39:51

您可以使用聚合函数和分组依据:

select u.name, sum(ms.money) 
from user u, money_spend ms
where u.userid = ms.userid 
group by u.userid

请注意,这里假设每个用户在 Money_spend 表中至少有 1 行: http://www.sqlite.org/lang_aggfunc.html

由于聚合函数的工作方式,您可以为每个用户设置值为 0 的 Money_spend 表,这样您就不会遇到有任何问题:)

You can use an aggregate function and group by:

select u.name, sum(ms.money) 
from user u, money_spend ms
where u.userid = ms.userid 
group by u.userid

Note that this here assumes that every user has at least 1 row in the money_spend table: http://www.sqlite.org/lang_aggfunc.html

Due to the way that aggregate functions work, you could set up the money_spend table with a 0 value for each user so you don't run into any issues :)

信仰 2024-10-14 13:39:51

因为您的用户可能在表 Money_spend 中没有任何条目,所以您需要一个外部联接:

select n.name, sum(ms.money)
from user n
left outer join money_spend ms on (n.userid = ms.userid)
group by n.name

编辑:为了确保这一切都有效,我刚刚尝试了这个

create table user(userid, name);
insert into user values (1, 'user1');
insert into user values (2, 'user2');
insert into user values (3, 'user3');
create table moneyspend(userid, amount);
insert into moneyspend values (1,10);
insert into moneyspend values (1,20);
insert into moneyspend values (2,100);
select * from user;
select * from moneyspend;
select u.name, sum(m.amount)
from user u
left outer join moneyspend m on (u.userid = m.userid)
group by u.name;
drop table user;
drop table moneyspend;

控制台输出如下(testSQLite 是我的测试数据库,testsql.sql 是上面的)

hols-Mac:div4 hol$ sqlite3 -init testsql.sql testSQLite
-- Loading resources from testsql.sql
1|user1
2|user2
3|user3
1|10
1|20
2|100
user1|30
user2|100
user3|
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> 

Because you might have users without any entry in table money_spend you need an outer join:

select n.name, sum(ms.money)
from user n
left outer join money_spend ms on (n.userid = ms.userid)
group by n.name

Edit: To be sure this all works I just tried this

create table user(userid, name);
insert into user values (1, 'user1');
insert into user values (2, 'user2');
insert into user values (3, 'user3');
create table moneyspend(userid, amount);
insert into moneyspend values (1,10);
insert into moneyspend values (1,20);
insert into moneyspend values (2,100);
select * from user;
select * from moneyspend;
select u.name, sum(m.amount)
from user u
left outer join moneyspend m on (u.userid = m.userid)
group by u.name;
drop table user;
drop table moneyspend;

The console output is the following (testSQLite is my test DB, testsql.sql is the above)

hols-Mac:div4 hol$ sqlite3 -init testsql.sql testSQLite
-- Loading resources from testsql.sql
1|user1
2|user2
3|user3
1|10
1|20
2|100
user1|30
user2|100
user3|
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文