We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,186 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

select count with complicated query

Hello :)
I'm working with a database of postcodes. I have an existsing query that returns the postcodes within a certain radius:

select *, acos(cos(51.496502411798 * (PI()/180)) *cos(-0.13982862499836 * (PI()/180)) *cos(lat * (PI()/180)) *cos(lng * (PI()/180))+cos(51.496502411798 * (PI()/180)) *sin(-0.13982862499836 * (PI()/180)) *cos(lat * (PI()/180)) *sin(lng * (PI()/180))+sin(51.496502411798 * (PI()/180)) *sin(lat * (PI()/180))) * 3959 as Dist from SW having Dist < 1 order by Dist

Dist 1 is the number of miles for the search radius.

I am making a script that will allow the user to enter a postcode and return a list of postcodes in the given radius. There can be over 20,000 results of which only 1000 will be displayed. Currently, I get all results into a PHP array and use the count() function to return the total, and just display upto 1000.
I would like to make the query produce only a count of relevant matches, and then make a follow up query with a limit of 1000 which will stop the unneccesary collection of data over 1000 rows.

Thanks for your time :D

2
Contributors
5
Replies
28 Minutes
Discussion Span
5 Months Ago
Last Updated
6
Views
Question
Answered
adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43
Solved Threads: 40
Skill Endorsements: 4

Just add LIMIT 1000 after your order by.

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

For a moment I thought I was being REALLY stupid :D
Goedendag Pritaeious, but that only returns 1000 results even if there are 25000 matches. I want to make an initial query that will tell me the exact amount of matches and no data, and then a second query collecting the data with a limit of 1000, or one that does both if it is possible.
Thanks for your speedy consideration :D

adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43
Solved Threads: 40
Skill Endorsements: 4

Ah, okay. Just use the original query and select only COUNT(*) AS resultcount. That way the server does the counting, but only one record gets transferred to your script.

select count(*) as resultcount
from SW 
where (acos(cos(51.496502411798 * (PI()/180)) *cos(-0.13982862499836 * (PI()/180)) *cos(lat * (PI()/180)) *cos(lng * (PI()/180))+cos(51.496502411798 * (PI()/180)) *sin(-0.13982862499836 * (PI()/180)) *cos(lat * (PI()/180)) *sin(lng * (PI()/180))+sin(51.496502411798 * (PI()/180)) *sin(lat * (PI()/180))) * 3959) < 1
pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

It's perfect!
Thank you Pritaeious, as usual, a fast and concise solution :D

adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43
Solved Threads: 40
Skill Endorsements: 4
Question Answered as of 5 Months Ago by pritaeas

If I were you I'd put them in two stored procedures. Keeps your code nice and readable.

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3017 seconds using 2.69MB