简体   繁体   English

MySQL子查询问题

[英]MySQL Subqueries issue

To make this example simple, lets pretend my table has the following attributes. 为了简化此示例,让我们假设我的表具有以下属性。

Table

ID: Int
Amount: Int
nameId: Int

Records Ex 记录前

ID: 1
Amount: 2
nameId: 3

ID: 2
Amount: 2
nameId: 3

ID: 3
Amount: 1
nameId: 3

Currently I have the following query. 目前,我有以下查询。

SELECT DISTINCT(amount) FROM server.`inventory` where nameid = 558

It gives me 它给我

Amount 1
Amount 2

Straight forward, it selects a unique records base on amount. 直截了当,它根据金额选择唯一的记录。 However, how can I also include in my query so that it counts how many id's are using that amount with nameid 558 但是,如何在查询中也包括在内,以便计算使用nameid 558的数量使用了多少id

So the output should be 所以输出应该是

Amount 1, Used 1
Amount 2, Used 2

This is an aggregation query. 这是一个聚合查询。 You want group by and count() : 您想要group bycount()

SELECT amount, COUNT(*)
FROM server.inventory 
WHERE nameid = 558
GROUP BY amount;

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

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