Jump to content

[SOLVED] query help - select distinct and sort


sandrob57

Recommended Posts

ok this is my current query. Not a good start, because there is already a syntax error, but it should give you an idea of what I want to do:

 

$result = dbquery("SELECT DISTINCT ip FROM fusion_ip LIMIT $rowstart,20");

 

Basicaly my website records every ip a user has logged on from and keeps it saved in a table called fusion_ip.

 

The columns for the table are ip_id (a auto increment id assigned to each ip), ip (the ip) and user_id (the id of the user the ip belongs to).

 

As an example, the db may look like this

 

ip_id__user_id___ip

1_____33______111.212.32.32

2_____1_______111.212.32.32

3_____1_______43.12.1.3

4_____5_______67.86.54.2

5_____2_______72.65.5.4

 

What I am trying to do is make a list of every IP and sort them by the most active. So the list would look like this:

 

[____IP____][_Users Using This Ip__]

43.432.424.2________4

5.4.32.1____________2

 

etc. etc.

 

So how the hell would I make a query capable of accomplishing this?

 

EDIT: im guessing a i need a SUM() somewhere in there, or an order by?

Link to comment
Share on other sites

Ok I came up with this (it doesnt work, but I feel it's a step in the right direction)

 

$result = dbquery("SELECT DISTINCT ip,SUM(ip) AS ip_count FROM fusion_ip LIMIT $rowstart,20 ORDER BY ip_count, DESC");

 

edit: still cant get it to work  ???  :-[

Link to comment
Share on other sites

Why are you summing the IP, that will essentially add up the ips together...

 

$result = dbquery("SELECT ip, COUNT(ip) AS ip_count FROM fusion_ip GROUP BY ip ORDER BY ip_count DESC LIMIT $rowstart,20");

 

Not sure if my sql is correct, but that is the idea, you want to use the GROUP BY Expression with the COUNT expression

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.