Solved

How to use MySQL to Limit secondary results

Posted on 2011-03-10
11
530 Views
Last Modified: 2012-05-11
I've been thinking on this one and it's stumped me.  

I need to query a user table (dhcpclients) for all users (31 records) and include 25 rows for each user from another table (logs). So the end result is 775 rows- 25 for each 31. Make sense?

Attached is the code I'm working with.  

The query that makes up the "join" in there works fine but obviously stops at the limit- which is this case would be one user record with 25 of the log table records. the problem is if I move the limit up it's not going to move to the next user until it's exhausted all the matches in the log table. I need it to quit after 25 and move to the next user.

Make sense?


SELECT dhcp.machinename, log1.* FROM dhcpclients dhcp
JOIN (
SELECT DATE(logs.date) AS 'Date', SUBSTRING_INDEX(SUBSTRING_INDEX(logs.request,'://',-1),'/',1) AS 'Site',
COUNT(logs.request) AS 'Hits',
FORMAT(SUM(logs.elapsed)/1000,2) AS 'Xfr Time(sec)', FORMAT(SUM(logs.bytes)/1024,2) AS 'KB Txfrd',
FORMAT((((SUM(logs.bytes)/1024)/1024)*8)/(SUM(logs.elapsed)/1000),2) AS 'Mbits/s', logs.ip
FROM logs 
WHERE logs.request NOT LIKE '[%' AND SUBSTRING_INDEX(SUBSTRING_INDEX(logs.request,'://',-1),'/',1) NOT LIKE '%knothe.com%'
AND DATE(logs.date) = DATE(NOW())
GROUP BY DATE(logs.date),logs.ip, SUBSTRING_INDEX(SUBSTRING_INDEX(logs.request,'://',-1),'/',1)
ORDER BY DATE(logs.date) DESC, COUNT(logs.request)DESC, SUM(bytes)/1024 DESC
LIMIT 25 ) 

log1 ON log1.ip = dhcp.ipaddress 

ORDER BY dhcp.machinename ASC

Open in new window

0
Comment
Question by:digitalwav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 10

Expert Comment

by:APNFSSC
ID: 35101528
Does your logs table have a primary auto incrementing key?

In this thread a method is described but it looks to me that it requires the key to work.

http://forums.mysql.com/read.php?20,135077,135077#msg-135077

First post then result

http://forums.mysql.com/read.php?20,135077,135107#msg-135107

0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 35106525
Hi,

This works for MSSQL, but maybe you can make it work in MySQL ...

It's just an example with 2 tables, parent and child 1 to many.


Hope it helps
SELECT *  FROM 
	(SELECT RANK() OVER 
			(PARTITION BY Parent.ID ORDER BY Child.ID ASC) AS [RANK], Parent.ID, Child.ID as childID
		FROM XXXXX AS Parent
			INNER JOIN YYYYY AS Child
				ON Child.parentID = Parent.ID
	) AS TB
WHERE TB.[RANK] <= 25

Open in new window

0
 
LVL 1

Author Comment

by:digitalwav
ID: 35108107
Yes it has a auto increment key. I'll check out the thread you're referring to.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:digitalwav
ID: 35131028
Ok, I tried the having count(xx.xx) idea and the results don't make any sense.  The rank idea doesn't work unless the rank can reset after each change in column 'machine name'

This simply may not be possible in pure sql. I may have to do it in php.
0
 
LVL 1

Author Comment

by:digitalwav
ID: 35131052
Let me try to explain this another way. I want the top 25 sites by machine_name. In this case the query I have gives ALL sites by machine_name for the day. If I just add limit to the statement it limits the overall results to 25, not the result by machine_name.
0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 35132497
the idea of RANK is exactly what you want, I tried in my MS SQL database and it will return exactly what you need.

SELECT RANK() OVER
                  (PARTITION BY Parent.ID ORDER BY Child.ID ASC)

Gives a rank for every parentID so you will have
 
Parent   Child
1             1
1             2
1             3
2             1
2             2

and so on...

however I don't know how you can 'translate' this into MySql ...

Cheers
0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 35132536
Sorry, the name of the columns is not right, It'll be like this

Parent   [RANK]
1             1
1             2
1             3
2             1
2             2
0
 
LVL 1

Author Comment

by:digitalwav
ID: 35136972
I'll see what I can come up with. MySQL can do something similar to Rank. I'll show my work when done.
0
 
LVL 1

Author Comment

by:digitalwav
ID: 35137465
The rank idea doesn't work. The only rank method for mysql is basically a counter 'column' which results in each record getting a consecutively higher number.  There is no way to reset that counter that I can see when then parent column changes to a new value.
0
 
LVL 10

Accepted Solution

by:
APNFSSC earned 500 total points
ID: 35142554
digitalwav I think what you are after is a store procedure, so that you can loop through the subnets.

http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx

0
 
LVL 1

Author Closing Comment

by:digitalwav
ID: 35148027
yup, APNFSSC, that's what I'm going to have to do. Thanks for the pointer. I was hoping to avoid procedures.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question