简体   繁体   English

选择同一表中的行数

[英]Select count of rows from the same table

Hi I have a table as below and I need to select the count of different trade IDs for given distinct OrderIds I am unable to do so in MySQL format 嗨,我有一张下表,我需要为给定的不同OrderId选择不同的交易ID数,但我无法采用MySQL格式

+-----------+---------+
| OrderID   |TradeID  |
+===========+=========+
| 1         | 58761   |
+-----------+---------+
| 1         | 58762   | 
+-----------+---------+
| 2         | 58763   | 
+-----------+---------+
| 2         | 58764   | 
+-----------+---------+
| 2         | 58765   |
+-----------+---------+

Where result is required as 需要结果的地方

+-----------+---------+
| OrderID   |Count    |
+===========+=========+
| 1         | 2       |
+-----------+---------+
| 2         | 4       | 
+-----------+---------+

Just COUNT / GROUP BY 只需COUNT / GROUP BY

SELECT OrderID, COUNT(*) 
FROM some_table 
GROUP BY OrderID

SQL Fiddle here:- SQL小提琴在这里:-

http://www.sqlfiddle.com/#!9/305630/1 http://www.sqlfiddle.com/#!9/305630/1

Note that if you wanted a couple of distinct TradeIDs for each OrderID you could use 请注意,如果您想为每个OrderID使用几个不同的TradeID,则可以使用

SELECT OrderID, COUNT(DISTINCT TradeID) 
FROM some_table 
GROUP BY OrderID

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

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