简体   繁体   English

MYSQL选择不同的多列

[英]MYSQL Select Distinct, Multiple Columns

My SQL query looks like this - 我的SQL查询看起来像这样-

SELECT DISTINCT ip, title, url FROM stats

My goal is to select one row for each distinct ip , along with title and url ; 我的目标是为每个不同的ip选择一行,以及titleurl however I find when I add the title and url fields to my query it shows me all rows. 但是,当我将titleurl字段添加到查询中时,它会显示所有行。

Sample Data 样本数据

ip          title      url
---------------------------
127.0.0.1   title      url
127.0.0.2   title      url
127.0.0.1   difftitle  url

Result I would like 结果我想要

ip          title      url
---------------------------
127.0.0.1   title      url
127.0.0.2   title      url

I think what you are looking for is a query like this - 我认为您正在寻找的是这样的查询-

SELECT 
    ip,
    title,
    url
FROM 
    stats
GROUP BY 
    ip 

GROUP BY is similar to DISTINCT - it means that all results will be grouped by ip , so it will only show one row of results for each distinct ip . GROUP BYDISTINCT相似-这意味着所有结果都将按ip分组,因此对于每个不同的ip仅显示一行结果。 However, nothing determines which record will be returned (eg which title and url will be shown). 但是,没有什么决定将返回哪个记录(例如,将显示哪个titleurl )。

There is no 'first' entry in the database - a relational database has no 'order' as such, unless you choose to order by a field. 数据库中没有“第一”条目-关系数据库本身没有“订单”,除非您选择按字段排序。

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

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