CodeOverload
CodeOverload

Reputation: 48505

Distinct & Group SQL Query

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 )

I have the following table:

              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 |  

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 |

The Most important things:

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?

Tons of thanks.

Upvotes: 0

Views: 151

Answers (1)

Angelo Vargas
Angelo Vargas

Reputation: 2001

What you are wanting to do can be accomplished using group_contact.

Example

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

edit: forgot group by

Upvotes: 1

Related Questions