SELECT DISTINCT(id) vetid,
c.clinic_id,
c.clinic_name,
c.type,
c.city,
c.state,
c.country,
c.lat,
c.lng,
c.zip,
( 3959 * Acos( Cos( Radians(44.977498) ) * Cos( Radians( c.lat ) ) * Cos( Radians( c.lng ) - Radians(-93.406556) ) + Sin( Radians(44.977498) ) * Sin( Radians( c.lat ) ) ) ) AS distance
FROM biah_clinics c
WHERE opt_out != '1'
AND c.country = 'US'
HAVING distance < '10'
ORDER BY distance limit cast('20' AS int);
Using Drupal's db_query, all parameters passed to the query are passed as STRING. This is giving me an error. That is why in my query, I want to cast that parameter to INT.
So it should end up like this:
limit cast('20' AS int);
limit 20;
But this is giving me an error in MySQL:
"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'CAST('25' AS
INT)' at line 15"
I ended up using drupal's db_query_range(), due to sql limit doesn't work but with integer literals.
https://api.drupal.org/api/drupal/includes%21database%21database.inc/function/db_query_range/7.x
I want to use UNION to join two SQL SELECT queries. I need the final data to use the HAVING clause to filter the entire query. Here is my statement:
SELECT CLIENT,
BIZNAME,
BIZSTREET,
BIZCITY,
BIZSTATE,
BIZZIP,
BIZPHONE,
URL,
LAT,
LNG,
CONSOLIDATED,
( 3959 * ACOS(COS(RADIANS('%s')) * COS(RADIANS(LAT)) * COS(
RADIANS(LNG) - RADIANS('%s'))
+ SIN
(RADIANS('%s')) * SIN(RADIANS(LAT))) ) AS distance
FROM BizInfo
INNER JOIN WebSites
ON WebSites.CUSTOMER = BizInfo.CUSTOMER
WHERE BizInfo.CLIENT = 'GCB'
AND WebSites.STATUS <> 'Cancel'
AND WebSites.STATUS <> 'In Progress'
AND WebSites.STATUS <> 'Review'
AND WebSites.STATUS <> 'Testing'
UNION
SELECT CLIENT,
BIZNAME,
BIZSTREET,
BIZCITY,
BIZSTATE,
BIZZIP,
BIZPHONE,
'http://www.abc-site.com',
LAT,
LNG,
'0',
( 3959 * ACOS(COS(RADIANS('%s')) * COS(RADIANS(LAT)) * COS(
RADIANS(LNG) - RADIANS('%s'))
+ SIN
(RADIANS('%s')) * SIN(RADIANS(LAT))) ) AS distance
FROM BizInfo
WHERE CLIENT = 'GCB'
AND BIZNAME = 'Acme'
HAVING DISTANCE < '%s'
ORDER BY DISTANCE
LIMIT 0, 200
I read on this site http://www.really-fine.com/SQL_union.html (GROUP BY and HAVING clauses can be used only within individual queries and cannot be used to affect the final results set. ), but I don't understand how to implement this or if it is correct.
How do I properly write this SQL query?
You can probably solve that very easily by wrapping everything into a subquery. Something like :
SELECT
*
FROM
(
SELECT Client, BizName, BizStreet, BizCity, BizState, BizZip, BizPhone, url, lat, lng, Consolidated,
( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) )
AS distance FROM BizInfo
INNER JOIN WebSites ON WebSites.Customer = BizInfo.Customer
WHERE BizInfo.Client = 'GCB'
AND WebSites.Status <> 'Cancel' AND WebSites.Status <> 'In Progress' AND WebSites.Status <> 'Review' AND WebSites.Status <> 'Testing'
UNION SELECT Client, BizName, BizStreet, BizCity, BizState, BizZip, BizPhone, 'http://www.abc-site.com', lat, lng, '0',
( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) )
AS distance FROM BizInfo WHERE Client = 'GCB' AND BizName = 'Acme'
) AS ClientInfo
WHERE
distance < '%s'
ORDER BY
distance
LIMIT 0 , 200
This is the fastest path to what you want but it is not very clean.
Also please tell me that all those parameters are not vulnerable to SQL injection...?
HAVING can only be used in an aggregate query with a GROUP BY clause. Your queries do not. Use WHERE instead.
Those are scalar functions, not aggregate functions. So to filter on the result, use the WHERE clause, not the HAVING clause. You are filtering horizontally, not vertically.
You cannot use a column alias in the WHERE clause, so you have to restate the functions used in the SELECT list (I've done that in the query below).
Try the following. There are 2 issues at play.
SELECT Client,
BizName,
BizStreet,
BizCity,
BizState,
BizZip,
BizPhone,
url,
lat,
lng,
Consolidated,
(3959 * acos(cos(radians('%s')) * cos(radians(lat)) *
cos(radians(lng) - radians('%s')) +
sin(radians('%s')) * sin(radians(lat)))) AS distance
FROM BizInfo
INNER JOIN WebSites
ON WebSites.Customer = BizInfo.Customer
WHERE BizInfo.Client = 'GCB'
AND WebSites.Status <> 'Cancel'
AND WebSites.Status <> 'In Progress'
AND WebSites.Status <> 'Review'
AND WebSites.Status <> 'Testing'
UNION
SELECT Client,
BizName,
BizStreet,
BizCity,
BizState,
BizZip,
BizPhone,
'http://www.abc-site.com',
lat,
lng,
'0',
(3959 * acos(cos(radians('%s')) * cos(radians(lat)) *
cos(radians(lng) - radians('%s')) +
sin(radians('%s')) * sin(radians(lat)))) AS distance
FROM BizInfo
WHERE Client = 'GCB'
AND BizName = 'Acme'
and (3959 * acos(cos(radians('%s')) * cos(radians(lat)) *
cos(radians(lng) - radians('%s')) +
sin(radians('%s')) * sin(radians(lat)))) < '%s'
ORDER BY distance LIMIT 0, 200
I've wrote a query with DISTINCT but I still get duplicate records returned.
SELECT DISTINCT
`cuisine_types`.`id`,
`cuisine_types`.`name`,
`cuisine_types`.`image`,
(
SELECT group_concat(`tagname` separator ', ')
FROM `cuisine_tags`
WHERE `cuisine_type` = `cuisine_types`.`id`
) AS tags,
(
3959 * acos( cos( radians(" . $dLat . ") ) * cos( radians( gps_lat ) ) * cos( radians( gps_lon ) - radians(" . $dLon . ") ) + sin( radians(" . $dLat . ") ) * sin( radians( gps_lat ) ) )
) AS distance
FROM `company`
LEFT JOIN `cuisine_types`
ON
`company`.`cuisine_type_id` = `cuisine_types`.`id`
HAVING
distance < " .$dMiles
When I try using the GROUP BY function my query isn't working properly.
When I use GROUP BY I place it above my HAVING:
GROUP BY `cuisine_types`.`name`
Examples:
When I use it with these values:
$dLat = '52.779716';
$dLon = '21.84803';
$iKm = '30';
It returns:
id = 1
name = Snackbar
image =
tags = Patat, Snacks
distance = 17.4713944772963
When I use $iKm with 3000 it returns this row as well:
id = 1
name = Snackbar
image =
tags = Patat, Snacks
distance = 722.407714147792
So I get two records.
When I use this with groupby and $iKm = 30; It returns nothing. With a value of 3000 it returns one row. But I have one record with a distance of 17 miles so thats below 30.
This should fix your problem, the issue was the distance calculation blocked the GROUP BY function. By putting the equation in the HAVING itself, the problem seemed to be fixed. Sorry I can't explain it in more detail.
SELECT
`cuisine_types`.`name`,
`cuisine_types`.`id`,
`cuisine_types`.`image`
`c`.`gps_lat` as lat,
`c`.`gps_lon` as lon,
(SELECT group_concat(`tagname` separator ', ') FROM `cuisine_tags` WHERE `cuisine_type`=`cuisine_types`.`id`) as tags FROM `company` as c LEFT JOIN `cuisine_types` ON c.`cuisine_type_id`
= `cuisine_types`.`id` GROUP BY `cuisine_types`.`name` HAVING ( 3959 * acos( cos( radians(52.779716) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(21.84803) ) + sin( radians(52.779716) ) * sin( radians( lat ) ) ) ) < 2000;
Ok so my stored procedure is reading variables wrong in its MySQL query. The results I need are obtained fine when I just use the body of the proc. I would like to make this a stored procedure.
Here is the SP in SQLyog:
DELIMITER $$
USE `XXXXXXXXXXXXXXX`$$
DROP PROCEDURE IF EXISTS `getcusbyzip`$$
CREATE DEFINER=`XXXXXXXXXXXXXXX`#`%` PROCEDURE `getcusbyzip`(IN zipcode VARCHAR(30), IN radius VARCHAR(30))
BEGIN
SELECT C.CustomerName, C.MenuId
FROM Customers C
INNER JOIN (
SELECT ZIPCODE, ( 3959 * ACOS( COS( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * COS( RADIANS( LAT ) ) * COS( RADIANS( LNG ) - RADIANS( (
SELECT Z.LNG
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) + SIN( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * SIN( RADIANS( LAT ) ) ) ) AS distance
FROM ZipCodes
HAVING distance <radius
ORDER BY distance
) AS RelevantCodes ON ( C.ZIPCODE = RelevantCodes.ZIPCODE );
END$$
DELIMITER ;
Now there is only 1 record that should be returned because I have only 1 customer added for now until I get this type issue resolved. Here is where I have a breakthrough (please don't laugh, I am new to SQL entirely :) ).
I have tried changing the TYPE as well for the parameters. The zipcode broke after I changed it to an INT and I used this:
CALL getcusbyzip(08361,50)
and my resulting rows were all the rows in which "8361" were in the zipcode. Basically that is my only personal explanation as to why I received so many rows back.
When I use this with both params set to VARCHAR type:
CALL getcusbyzip(08361,50)
or
CALL getcusbyzip('08361',50)
or
CALL getcusbyzip(08361,'50')
you get the point.
I get 0 rows back when I should be getting 1 row back.
So now I think Iknow what my problem is and I might actually ask it hopefully. What is causing my SP to read the input differently as a parameter than as if I just did a query and manually put the parameters into the query. Secondly how can I adjust my procedure so that the MySQL server reads my procedure just like my Query but with the parameters ofcoarse?
Below is the query that works perfectly fine:
SELECT C.CustomerName, C.MenuId
FROM Customers C
INNER JOIN (
SELECT ZIPCODE, ( 3959 * ACOS( COS( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =08360
LIMIT 0 , 1
) ) ) * COS( RADIANS( LAT ) ) * COS( RADIANS( LNG ) - RADIANS( (
SELECT Z.LNG
FROM ZipCodes Z
WHERE Z.ZIPCODE =08360
LIMIT 0 , 1
) ) ) + SIN( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =08360
LIMIT 0 , 1
) ) ) * SIN( RADIANS( LAT ) ) ) ) AS distance
FROM ZipCodes
HAVING distance <50
ORDER BY distance
) AS RelevantCodes ON ( C.ZIPCODE = RelevantCodes.ZIPCODE )
EDIT:
Another point to note is that when I change the variables to direct information in the SP(Stored Procedure) that does not fix it either so it has to be something I missing in MySQL procedure logic. I honestly do not understand why the results are different just because its in a stored procedure.
I am trying to solve another problem here. I was told by a member that in order to find an answer to my problem I needed to change the query to match the SP. In order to do that I was told to declare variables so I am using variables like I am in the SP. my problem is I have read so much my eyes are hurting and I cannot figure out how to declare friggin variables.
Please help!!
This is what I have read so far:
resource 1,
resource 2,
resource 3, resource 4, resource 5
and I could go on an on. I am literally stuck. I am new to SQL and this is bugging me out. I do not have to use a SP but I would really like to so I can learn the dos and donts and hopefully learn enough to help others.
DECLARE zipcode VARCHAR(30)
DECLARE radius VARCHAR(30)
SET zipcode = 08360
SET radius = 50
SELECT C.CustomerName, C.MenuId
FROM Customers C
INNER JOIN (
SELECT ZIPCODE, ( 3959 * ACOS( COS( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * COS( RADIANS( LAT ) ) * COS( RADIANS( LNG ) - RADIANS( (
SELECT Z.LNG
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) + SIN( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * SIN( RADIANS( LAT ) ) ) ) AS distance
FROM ZipCodes
HAVING distance <radius
ORDER BY distance
) AS RelevantCodes ON ( C.ZIPCODE = RelevantCodes.ZIPCODE )
I have tried so many combinations from what I have seen on the internet that I am starting to think its just too late in the night to do anything more. If you take a look at some of the resources I looked at you will see where I saw the difference between with '#' and without.
If you want to create a stored proc, you'll need to use the proper syntax.
See: http://dev.mysql.com/doc/refman/5.5/en/create-procedure.html
Note that inside the proc body you'll need to terminate each and every statement with a ;.
Because you'll need to let MySQL know when the body proc ends and you'll need to redeclare the delimiter into something that not a ;.
This will prevent MySQL from interpreting your stored proc after the first ';', but instead listen all the way up to the $$.
Don't forget to revert the delimiter back to the default after you're done inputting the function.
SET DELIMITER $$
CREATE PROCEDURE QueryCustomerMenu(IN zipcode VARCHAR(30), IN radius VARCHAR(30))
BEGIN
DECLARE somevar VARCHAR(20);
SET somevar = '456';
SELECT C.CustomerName, C.MenuId
FROM Customers C
INNER JOIN (
SELECT ZIPCODE, ( 3959 * ACOS( COS( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * COS( RADIANS( LAT ) ) * COS( RADIANS( LNG ) - RADIANS( (
SELECT Z.LNG
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) + SIN( RADIANS( (
SELECT Z.LAT
FROM ZipCodes Z
WHERE Z.ZIPCODE =zipcode
LIMIT 0 , 1
) ) ) * SIN( RADIANS( LAT ) ) ) ) AS distance
FROM ZipCodes
HAVING distance <radius
ORDER BY distance
) AS RelevantCodes ON ( C.ZIPCODE = RelevantCodes.ZIPCODE );
END $$
SET DELIMITER ;
Now you can call the stored proc with:
CALL QueryCustomerMenu('09210','20');
And it will return a resultset with the CustomerName and MenuID's.