需要SQL语句

use*_*255 2 sql ms-access

我有3个表玩家表,它的武器表和虚拟表'拥有武器'.
player table有player_id,player_name,武器表有weapon_id,weapon_name,weapon_cost.拥有武器表有player_id,weapon_id.

球员表

player_id      player_name
1               mr.A
2               mr.B
Run Code Online (Sandbox Code Playgroud)

武器表

weapon_id      weapon_name       weapon_cost
1              sniper            100
2              gun               120
3              hummer            90
Run Code Online (Sandbox Code Playgroud)

拥有武器表

player_id      weapon_id
1              1
1              2
1              3
2              2
2              3
Run Code Online (Sandbox Code Playgroud)

我尝试了一个sql语句,用于显示玩家名称,他持有的武器总数以及他目前持有的所有武器的总成本......但是错误的出现就像"玩家名称不是聚合函数的一部分"我使用过计数和求和功能.但是出现了错误.请帮助我

Tar*_*ryn 6

首先,您需要加入表格.其次,由于您使用的是聚合函数,因此您需要GROUP BY在不在聚合函数中的列上使用子句.

因此,您的查询将类似于以下内容:

select p.player_name,
  count(w.weapon_id) as TotalWeapons,
  sum(w.weapon_cost) as TotalCost
from ([player] as p
inner join [Possess_Weapon] as pw
  on p.player_id = pw.player_id)
inner join [weapon] as w
  on pw.weapon_id = w.weapon_id
group by p.player_name
Run Code Online (Sandbox Code Playgroud)

注意,当查询中存在多个连接时,您将看到MS Access在连接周围需要括号.

演示.该演示是SQL Server,但已经在MS Access中进行了测试,并返回了结果:

| PLAYER_NAME | TOTALWEAPONS | TOTALCOST |
------------------------------------------
|        mr.A |            3 |       310 |
|        mr.B |            2 |       210 |
Run Code Online (Sandbox Code Playgroud)