简体   繁体   English

基本SQL和,选择,位置,GROUP BY语句

[英]Basic SQL Sums, Selects, WHERES, GROUP BY statements

I'm currently working on this data. 我目前正在处理此数据。

What should be resulting: 应该是什么结果:

在此处输入图片说明

What should be produced is the result at the bottom of that picture. 应该产生的是该图片底部的结果。

Instead, I'm getting the result: 相反,我得到的结果是:

在此处输入图片说明

My code is the following: 我的代码如下:

SELECT distinct(username) as 'Name', p.price as 'Price', p.quantity as 'Quantity', (p.price * p.quantity) as 'Total'
 FROM users u, purchase p
WHERE u.userID = p.userID 
ORDER BY (username) asc  ; 

I am wondering how would I go about adding up the unique usernames (Alice's 4 and 1.5 to produce 5.5) and how do I also get Matt's name to display on the data? 我想知道如何将唯一的用户名相加(爱丽丝的4和1.5生成5.5),又如何让马特的名字显示在数据上?

Just minor changes, but the important thing is to group by user and not order by user 只是微小的更改,但重要的是按用户分组而不是按用户排序

SELECT username as 'Name', SUM(p.price * p.quantity) as 'Total'
 FROM users u, purchase p
WHERE u.userID = p.userID 
GROUP BY username; 

从[用户]中选择u.name,总和(数量*价格)u左加入u.name对u.id = p.userId组购买p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM