简体   繁体   English

在子查询中使用UNION ALL分组

[英]GROUP BY in SUBQUERY WITH UNION ALL

I have two tables, one showing the search queries history, the other showing the locations. 我有两个表,一个显示搜索查询的历史记录,另一个显示位置。

The first table is for the search queries: searchTerm | 第一个表用于搜索查询:searchTerm | date | 日期| userId | userId | historyId The second is for the locations: userId | historyId第二个用于位置:userId | date | 日期| historyId | historyId | lat | lat | lng LNG

I am using UNION ALL to bring these together in a sub query with JOIN LEFT & JOIN RIGHT to create a FULL JOIN. 我正在使用UNION ALL将这些组合在一起,并在一个子查询中使用JOIN LEFT和JOIN RIGHT来创建FULL JOIN。

SELECT st, fa FROM
                 (SELECT webHistory.searchTerm AS st, locationHistory.fullAddress AS fa
                  FROM webHistory LEFT OUTER JOIN locationHistory
                  ON webHistory.historyId=locationHistory.historyId
                  UNION ALL
                  SELECT webHistory.searchTerm as st, locationHistory.fullAddress AS fa
                  FROM webHistory RIGHT OUTER JOIN locationHistory
                  ON webHistory.historyId=locationHistory.historyId) t

Every thing is working beautifully I'm getting results for just entries in webHistory table, and the same for the locationHistory table. 一切正常,我得到的只是webHistory表中条目的结果,而locationHistory表也是如此。 The issue I have is when I'm getting rows that have been added to both. 我遇到的问题是当我获取已添加到两者中的行时。 They show twice! 他们显示两次!

I can't just use union as it will JOIN more than I want it to. 我不能只使用工会,因为它会加入比我想要的更多的工会。 If two of the same searches are added, instead of showing two it will join them into one. 如果添加了两个相同的搜索,则不会显示两个搜索,而是会将它们合并为一个。 So I need to use UNION ALL. 所以我需要使用UNION ALL。

I have to use union as I'm JOINing from the LEFT and RIGHT and in mySql FULL JOIN isn't supported. 我必须使用工会,因为我是从左向右加入,而在mySql中,不支持FULL JOIN。

So I'm assuming I need to GROUP BY historyId as this id will always be the same on rows added at the same time (when a search is made by query and location). 所以我假设我需要GROUP BY historyId,因为在同时添加的行上(当通过查询和位置进行搜索时)此id始终是相同的。 Problem is, i've tried and tested but I can't seem to get this to work. 问题是,我已经尝试和测试了,但是我似乎无法使它正常工作。 Am I missing something? 我想念什么吗?

Lastly, while I'm asking a question and because I've not yet got to the point of testing this would the ORDER BY date be on the OUTER or SUBQUERIES as I want to show the most recent as the first result? 最后,当我问一个问题时,因为我还没有进行测试,这是因为ORDER BY date位于OUTER或SUBQUERIES上,因为我想显示最新的结果作为第一个结果?

/* More Information */ /* 更多信息 */

I need to to output: 我需要输出:

SearchTerm if there is no fullAddress. SearchTerm(如果没有fullAddress)。 Full address if there is no searchTerm. 如果没有searchTerm,则为完整地址。 SearchTerm & fullAddress if there is both. SearchTerm和fullAddress(如果同时存在)。

Currently it outputs the first two fine, but the latter will output twice. 目前,它会输出前两个罚款,但后者将输出两次。

As far as understand, you want duplicates with the same historyId to be eliminated, so you should be able to use UNION as long as the historyId is included in the UNION so that duplicates with another historyId won't be eliminated; 据了解,要被淘汰具有相同historyId重复,所以你应该能够使用UNION只要historyId包括在UNION使之与另一 historyId重复不会被淘汰;

SELECT st, fa FROM
 (SELECT webHistory.searchTerm AS st, 
         locationHistory.fullAddress AS fa, 
         webHistory.historyId
  FROM webHistory LEFT OUTER JOIN locationHistory
  ON webHistory.historyId=locationHistory.historyId
  UNION
  SELECT webHistory.searchTerm as st, 
         locationHistory.fullAddress AS fa,
         locationHistory.historyId
  FROM webHistory RIGHT OUTER JOIN locationHistory
  ON webHistory.historyId=locationHistory.historyId) t

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

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