Last active
December 14, 2015 19:09
-
-
Save tgmweb/5134327 to your computer and use it in GitHub Desktop.
Branch Locator SQL Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfquery name="geteverything" datasource="bmf" result="info"> | |
SELECT | |
organisation.*, | |
address.* | |
<cfif radius neq "" AND lat neq "" AND lng neq ""> <!--- they are searching by location ---> | |
, | |
( | |
3959 * acos( | |
cos( | |
radians('#lat#') <!--- passed in latitude from client side script ---> | |
) | |
* | |
cos( | |
radians(latitude) | |
) | |
* | |
cos( | |
radians(longitude) - radians('#lng#') <!--- passed in longitude from client side script ---> | |
) | |
+ | |
sin( | |
radians('#lat#') <!--- passed in latitude from client side script ---> | |
) | |
* | |
sin( | |
radians(latitude) | |
) | |
) | |
) AS distance | |
</cfif> | |
FROM | |
organisation, | |
address | |
<cfif activity neq ""> <!--- they are filtering by activity ---> | |
, | |
activity, | |
orgCat | |
</cfif> | |
WHERE | |
organisation.source != 'WEBEX' | |
AND | |
address.addressID = organisation.addressID | |
<cfif companyName neq ""> <!--- they are searching by name ---> | |
AND | |
organisation.name LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#companyName#%"> | |
</cfif> | |
<cfif radius neq ""> <!--- they want results within a certain radius ---> | |
AND | |
address.latitude is not null | |
</cfif> | |
AND | |
status IN (<cfqueryparam cfsqltype="cf_sql_varchar" list="true" value="#status#">) | |
<cfif activity neq ""><!--- they are filtering by activity ---> | |
AND | |
activity.id IN (<cfqueryparam cfsqltype="cf_sql_varchar" value="#activity#" list="true">) | |
AND | |
orgCat.activityValue= activity.id | |
AND | |
organisation.orgNumber = orgCat.orgID | |
</cfif> | |
<cfif radius neq "" AND lat neq "" AND lng neq ""><!--- they want results within a certain radius ---> | |
HAVING | |
distance < '#radius#' | |
</cfif> | |
ORDER BY | |
<cfif radius neq "" AND lat neq "" AND lng neq ""><!--- only order by distance if they are searching by location ---> | |
distance, | |
</cfif> | |
name; | |
</cfquery> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment