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

时间:2015-07-16 17:54:06

标签: mysql

我正在尝试选择卖家分组的COUNT(*)及其最新产品及其添加日期。但是,出于某种原因,它似乎按创建日期继续订购交易,即使我尝试子查询以防止这种情况也会提升。这是一个示例表:

------------------------------------------------
| 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 |
------------------------------------------------

我希望返回以下内容:

-----------------------------------------------------------
| 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 |
-----------------------------------------------------------

我目前的查询是:

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

但这似乎不起作用。有没有人有建议?

修改 谢谢大家的答案。对我来说非常奇怪的是,虽然它们似乎在SQL小提琴中工作,但它们在我的数据库服务器上失败了。例如,在服务器上使用以下内容提供以下内容:

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 |
+------+----------+----------+---------------------+-----------------+

虽然在SQL小提琴上运行相同的东西按预期工作。此外,关于JOIN的评论应该有效,但我遇到同样的问题,它会返回意外的结果。我的MySQL版本是5.5.37-MariaDB-34.0。关于这个的任何想法?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您需要指定希望mysql返回计数内容的字段名称:

select count(*) as nro from table;

在你的情况下:

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

但是对于所有解决方案,您需要为此部分查询创建别名" SELECT * FROM products ORDER BY CreateDate DESC"

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

和第二个查询:

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

必须工作。

答案 2 :(得分:0)

我是在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