Eliminating duplicate values based on only one column of the table

前端 未结 3 966
野的像风
野的像风 2020-11-28 07:06

My query:

SELECT sites.siteName, sites.siteIP, history.date
FROM sites INNER JOIN
     history ON sites.siteName = history.siteName
ORDER BY siteName,date
         


        
3条回答
  •  天涯浪人
    2020-11-28 07:43

    From your example it seems reasonable to assume that the siteIP column is determined by the siteName column (that is, each site has only one siteIP). If this is indeed the case, then there is a simple solution using group by:

    select
      sites.siteName,
      sites.siteIP,
      max(history.date)
    from sites
    inner join history on
      sites.siteName=history.siteName
    group by
      sites.siteName,
      sites.siteIP
    order by
      sites.siteName;
    

    However, if my assumption is not correct (that is, it is possible for a site to have multiple siteIP), then it is not clear from you question which siteIP you want the query to return in the second column. If just any siteIP, then the following query will do:

    select
      sites.siteName,
      min(sites.siteIP),
      max(history.date)
    from sites
    inner join history on
      sites.siteName=history.siteName
    group by
      sites.siteName
    order by
      sites.siteName;
    

提交回复
热议问题