简体   繁体   English

MySQL选择COUNT(*)和具有最大日期的行

[英]MySQL Select COUNT(*) and Row with Max Date

I am trying to select the COUNT(*) of deals grouped by seller along with their most recent product and the date that it was added. 我正在尝试选择按卖家分组的交易的COUNT(*)个及其最近的产品及其添加日期。 However, for some reason it seems to keep ordering the deals by creation date, ascending even when I try subqueries to prevent that. 但是,由于某种原因,它似乎会继续按创建日期对交易进行排序,即使我尝试使用子查询来防止这种情况,该交易也会不断上升。 Here is an example table: 这是一个示例表:

------------------------------------------------
| ID | Provider |      URL        | CreateDate |
------------------------------------------------
| 1  | Prov1    | http://ex.com/1 | 2015-05-10 |
| 2  | Prov1    | http://ex.com/2 | 2015-06-10 |
| 3  | Prov1    | http://ex.com/3 | 2015-07-10 |
| 4  | Prov2    | http://ex.com/4 | 2015-05-10 |
| 5  | Prov2    | http://ex.com/5 | 2015-06-10 |
------------------------------------------------

I am looking to return the following: 我希望返回以下内容:

-----------------------------------------------------------
| ID | COUNT(*) | Provider |      URL        | CreateDate |
-----------------------------------------------------------
| 3  |    3     | Prov1    | http://ex.com/3 | 2015-07-10 |
| 5  |    2     | Prov2    | http://ex.com/5 | 2015-06-10 |
-----------------------------------------------------------

My current query is: 我当前的查询是:

SELECT ID,COUNT(*),Provider,CreateDate,URL
FROM (SELECT * FROM products ORDER BY CreateDate DESC)
GROUP BY Provider;

But this doesn't seem to work. 但这似乎不起作用。 Does anyone have a suggestion? 有人有建议吗?

EDIT Thank you all for the great answers. 编辑谢谢大家的出色回答。 What is extremely strange to me is that while they seem to work in a SQL fiddle, they fail on my database server. 对我而言,极为奇怪的是,尽管它们似乎可以在SQL提琴中工作,但它们却在我的数据库服务器上失败。 For example using the following on the server provides the following: 例如,在服务器上使用以下命令可提供以下功能:

mysql> INSERT INTO products
-> (`ID`, `Provider`, `URL`, `CreateDate`)
-> VALUES
-> (1, 'Prov1', 'http://ex.com/1','2015-05-10'),
-> (2, 'Prov1', 'http://ex.com/2','2015-06-10'),
-> (3, 'Prov1', 'http://ex.com/3','2015-07-10'),
-> (4, 'Prov2', 'http://ex.com/4','2015-05-10'),
-> (5, 'Prov2', 'http://ex.com/5','2015-06-10')
-> ;
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> SELECT ID,COUNT(*),Provider,CreateDate,URL
    -> FROM (SELECT * FROM products ORDER BY CreateDate DESC) t1
    -> GROUP BY Provider;
+------+----------+----------+---------------------+-----------------+
| ID   | COUNT(*) | Provider | CreateDate          | URL             |
+------+----------+----------+---------------------+-----------------+
|    1 |        3 | Prov1    | 2015-05-10 00:00:00 | http://ex.com/1 |
|    4 |        2 | Prov2    | 2015-05-10 00:00:00 | http://ex.com/4 |
+------+----------+----------+---------------------+-----------------+

While running the same thing on SQL fiddle works as expected. 在SQL小提琴上运行相同的东西时,按预期工作。 Further the comment regarding the JOIN should work, but I am having the same issue with it returning unexpected results. 进一步,关于JOIN的评论应该起作用,但是我遇到同样的问题,它返回了意外的结果。 My version of MySQL is 5.5.37-MariaDB-34.0. 我的MySQL版本是5.5.37-MariaDB-34.0。 Any ideas on this? 有什么想法吗?

SELECT p1.*, COUNT(*)
FROM products p1
JOIN
(
    select provider, max(CreateDate) as m_date
    from products 
    group by provider
) p2 on p1.provider = p2.provider and p1.CreateDate = p2.m_date

You need specify the field name where you want mysql return count content: 您需要在想要mysql返回计数内容的地方指定字段名称:

select count(*) as nro from table;

in your case: 在您的情况下:

SELECT ID,COUNT(*) as nro,Provider,CreateDate,URL
FROM (SELECT * FROM products ORDER BY CreateDate DESC)
GROUP BY Provider;

but for all solution you need create an alias for this part of query "SELECT * FROM products ORDER BY CreateDate DESC" 但对于所有解决方案,您都需要为此查询“ SELECT * FROM products ORDER BY CreateDate DESC”的这一部分创建别名

CREATE VIEW p AS (SELECT * FROM products ORDER BY CreateDate DESC);

and the second query: 第二个查询:

  SELECT ID,COUNT(*) as nro,Provider,CreateDate,URL
  FROM (p)
  GROUP BY Provider;

must be work. 必须工作。

I did it in SQL Server but the query in mysql works. 我是在SQL Server中完成的,但mysql中的查询有效。

Create TABLE #Products(
Id int,
Provider varchar(50),
URL Varchar(300),
Createdate varchar(50)
)


INSERT INTO #Products

SELECT 1,'Prov1','http://ex.com/1 ','2015-05-10'  UNION ALL
SELECT 2,'Prov1','http://ex.com/2 ','2015-06-10'  UNION ALL
SELECT 3,'Prov1','http://ex.com/3 ','2015-07-10'  UNION ALL
SELECT 4,'Prov2','http://ex.com/4 ','2015-05-10'  UNION ALL
SELECT 5,'Prov2','http://ex.com/5 ','2015-06-10' 




SELECT p1.id, p2.Provider,p2.m_date,p1.url, COUNT(*)
FROM #products p1
JOIN
(
    select  provider, max(CreateDate) as m_date
    from #products 
    group by provider
) p2 on p1.provider = p2.provider and p1.CreateDate = p2.m_date
GROUP BY p1.id, p2.Provider,p2.m_date,p1.url

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

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