与众不同组SQL查询 [英] Distinct & Group SQL Query

查看:82
本文介绍了与众不同组SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个比较复杂的MySQL查询,我不知道如何编写它而不消耗大量内存。 (我不太介意)

I have a little bit complex MySQL query for me and i can't figure out how to write it without eating tons of memory. ( i don't really mind if it would )

我有下表:

              TABLE: users(id,ip)
              ------------------------------------
              4BF1510 | 40.145.10.99 <-- SAME IP (A)
SAME ID   --> 510SD55 | 65.12.105.42 
SAME ID   --> 510SD55 | 45.184.15.10
              201505V | 40.145.10.99 <-- SAME IP (A)
              984AZ6C | 72.98.45.76
                      | 10.15.78.10  <-- SAME IP (B)
                      | 10.15.78.10  <-- SAME IP (B)
SAME ID   --> B1D56SX |  
SAME ID   --> B1D56SX |  

我想要一个查询,每个唯一的id或ip只获取1个项目,所以:

I want a query that only fetchs 1 item per a unique id or ip, So:

4BF1510 | 40.145.10.99
510SD55 | 65.12.105.42
984AZ6C | 72.98.45.76
        | 10.15.78.10
B1D56SX |

最重要的事情:


  • 如果多个条目具有相同的IP和相同的ID->将被分组

  • 如果多个条目具有相同的IP但具有不同的ID->它们不会分组

  • 如果多个条目具有相同的ID但IP不同->将它们分组

  • 如果多个条目具有相同的IP但 empty ID字段->将被分组

  • 如果多个条目具有相同的ID,但 empty IP字段->

  • If multiple entries have the same IP and same ID -> they'll be grouped
  • If multiple entries have the same IP but different IDs -> they'll not be grouped
  • If multiple entries have the same ID but different IPs -> they'll be grouped
  • If multiple entries have the same IP but empty ID fields -> they'll be grouped
  • If multiple entries have the same ID but empty IP fields -> they'll be grouped

这意味着该ID应该具有信任的第一优先级,因为多个用户可能使用相同的IP。

Which means that the ID should have the first priority of trust, Because multiple users may use the same IP.

有什么有效的方法来实现这一目标?

Any effective way to achieve that?

多谢。

推荐答案

您可以使用 group_contact

示例


SELECT ID, GROUP_CONCAT (ip)作为ips FROM logs 按ID分组

SELECT id, GROUP_CONCAT(ip) as ips FROM logs group by id

编辑:忘记分组依据

这篇关于与众不同组SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆