简体   繁体   English

MySQL DISTINCT在GROUP之前

[英]MySQL DISTINCT before GROUP

I have a MySQL table with 10 fields. 我有一个包含10个字段的MySQL表。 For this particular query, I only care about 4: Title, Variables, Location, Date. 对于此特定查询,我只关心4:标题,变量,位置,日期。 I would like to take the distinct values of these four groups and then group by Title, Variables. 我想采用这四个组的不同值,然后按标题,变量进行分组。 However, When I use the following query 但是,当我使用以下查询时

Select DISTINCT 
       Title, 
       Variables, 
       Location, 
       Date 
  FROM ForecastsTest2 WHERE ...
GROUP BY Variables, Title 
ORDER BY Title

It groups first and then takes distinct results. 它首先分组,然后得出不同的结果。 Is there any way I can switch this order? 有什么办法可以切换此顺序?

I ended up finding my solution in 我最终找到了解决方案

SELECT Variables, 
       Title 
  FROM (SELECT DISTINCT Variables, 
                        Title, 
                        Location, 
                        Date 
       FROM MyTABLE as Table1
       ) as Table2
       WHERE ...
       GROUP BY Variables, Title
       ORDER BY TITLE

I guess I didn't do a good job of mentioning this, but I also added a HAVING COUNT(*) >= 2 in the query. 我想我没有很好地提到这一点,但我还在查询中添加了HAVING COUNT(*) >= 2 In this case, the the count will happen after all non distinct rows have been removed. 在这种情况下,将在删除所有非唯一行之后进行计数。

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

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