sql syntax error while executing query (sql injection)? - mysql

QUERY:
model.client.query("SELECT ( 6371 * acos( cos( radians(:latitude) ) * cos( radians( latitude ) ) * cos( radians(longitude ) - radians(:longitude) ) + sin( radians(:latitude) ) * sin( radians( latitude) ) ) ) AS distance FROM offers where isActive= :isActive ",{'latitude': latitude, 'longitude': longitude,'isActive':1},function (err,rows) {
console.log(err);
});
ERROR
{ Error: ER_PARSE_ERROR: 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 ':latitude) ) * cos( radians( latitude ) ) * cos( radians(longitude ) - radians(:' at line 1

model.client.query("SELECT ( 6371 * acos( cos( radians(?) ) * cos( radians( latitude ) ) * cos( radians(longitude ) - radians(?) ) + sin( radians(?) ) * sin( radians( latitude) ) ) ) AS distance FROM offers where isActive= ? ",[latitude,longitude,latitude,isActive],function (err,rows) {
console.log(err);
});

Related

How to use Haversine formula in Sequelize-typescript to get distance between two coordinates

SELECT id,
( 6371 *
ACOS(
COS( RADIANS( db_latitude ) ) *
COS( RADIANS( $user_latitude ) ) *
COS( RADIANS( $user_longitude ) -
RADIANS( db_longitude ) ) +
SIN( RADIANS( db_latitude ) ) *
SIN( RADIANS( $user_latitude) )
)
)
AS distance FROM the_table HAVING distance <= the_table.SOME_COLUMN ORDER BY distance ASC"
I want to convert this query in sequelize-typescript.

SQL - Unqiue results based on criteria

Making a dealer locator where people search for a dealer near them. I want it to work in such a way that if a dealer has multiple branches near the person, only the closest branch shows. So the "name" field should be unique in the results with the result shown having the least distance to the person searching versus other rows that have the same "name" field. I also want only the closest 5 dealers shown ordered by their dealer level, or medal. Right now I have the following:
$query = sprintf("SELECT
name, address, contact, image, medal, phone, email, website, lat, lng,
( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
FROM dealers
HAVING distance < 60
ORDER BY medal, distance
LIMIT 0 , 5",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);
From what I've read, it sounds like I need to do something like:
SELECT * FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY name ORDER BY distance) AS num
FROM dealers)a
WHERE a.num = 1
Or something like that, but I can't get it to work right. Any insights on how I can get this to function would be greatly appreciated.
Get the minimum distance per dealer and of these take the first five. Then select again from the table in order to get complete records, but take only those records already identified by dealer and distance.
A WITH clause would be helpful, but MySQL doesn't support it. Well, ...
SELECT
name, address, contact, image, medal, phone, email, website, lat, lng,
( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
FROM dealers
WHERE (name, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) )) IN
(
SELECT
name, min( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
FROM dealers
GROUP BY name
HAVING distance < 60
ORDER BY distance
LIMIT 5
)
ORDER BY distance, medal;
Final Solution was:
SELECT name, address, contact, image, medal, phone, email, website, lat, lng,
( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
FROM dealers
WHERE (name,
( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) )
IN
(SELECT name, distance
FROM (SELECT name,
MIN( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
FROM dealers
WHERE (3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) < 90
GROUP BY name)
t)
ORDER BY medal, distance
LIMIT 0, 5",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat)
Thank you Thorsten Kettner, for putting me on the right track!

mysql - can't get query to work

I can't for the life of me see what is going wrong here. Its most likely something stupid, but I'm blind to it currently! I have a query:
SELECT
Links.Title,
(6371 * acos( cos( radians(43.4347229) ) * cos( radians( Links.Latitude ) ) * cos( radians( Links.Longitude ) - radians(6.737222195) ) + sin( radians(43.4347229) ) * sin( radians( Links.Latitude ) ) ) AS distance
FROM CatLinks,Links WHERE CatLinks.LinkID = Links.ID AND (Links.ID IN (16650,17190,153344) AND Links.isValidated = 'Yes' AND Links.PropertyType IN (1,2,3) AND Links.priceSort <= '9999' AND Links.PropertyType IN (1,2,3) AND Links.priceSort < '9999') ORDER BY distance LIMIT 0,50
..to which I get an error:
Error: Could not execute query: Failed to execute query: 'SELECT
Links.Title, (6371 * acos( cos( radians(43.4347229) ) * cos( radians(
Links.Latitude ) ) * cos( radians( Links.Longitude ) -
radians(6.737222195) ) + sin( radians(43.4347229) ) * sin( radians(
Links.Latitude ) ) ) AS distance FROM CatLinks,Links WHERE
CatLinks.LinkID = Links.ID AND (Links.ID IN (16650,17190,153344))
ORDER BY distance LIMIT 0,50': 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 'AS distance FROM CatLinks,Links WHERE
CatLinks.LinkID =' at line 3
Even trimming it down to a much simpler query, doesn't want to work:
SELECT
glinks_Links.Title,
(6371 * acos( cos( radians(43.4347229) ) * cos( radians( glinks_Links.Latitude ) ) * cos( radians( glinks_Links.Longitude ) - radians(6.737222195) ) + sin( radians(43.4347229) ) * sin( radians( glinks_Links.Latitude ) ) ) AS distance
FROM glinks_CatLinks,glinks_Links ORDER BY distance LIMIT 0,50
I've been going round and round with this issue all morning, so any advice would be much appreciate!
You forgot to close a bracket (near sin( radians( Links.Latitude ) ) ) )). Try this
SELECT
Links.Title,
(
6371 * acos(
cos(radians(43.4347229)) * cos(radians(Links.Latitude)) * cos(
radians(Links.Longitude) - radians(6.737222195)
) + sin(radians(43.4347229)) * sin(radians(Links.Latitude))
)
) AS distance
FROM
CatLinks,
Links
WHERE
CatLinks.LinkID = Links.ID
AND (
Links.ID IN (16650, 17190, 153344)
AND Links.isValidated = 'Yes'
AND Links.PropertyType IN (1, 2, 3)
AND Links.priceSort <= '9999'
AND Links.PropertyType IN (1, 2, 3)
AND Links.priceSort < '9999'
)
ORDER BY
distance
LIMIT 0,50

Error 1064 in SQL statement

I have tried to run a SQL command but I am receiving this error:
#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 ')
The SQL statement is
SELECT * ,
Match(product.title) against (query) + match(product.description) against (query1) +match(product.keyword) against (query2)+match(product.url) against (query2) + (average(review.rating)/100*count(productid))+(100/price)+(site.domainauthority/25)+(10-
CASE
WHEN lcase(seller.city) = lcase(city) THEN TRUNCATE(( 6371 * acos( cos( radians( 23 ) ) * cos( radians( seller.latitude ) ) * cos( radians( seller.longitude ) - radians(24) ) + sin( radians( 24 ) ) * sin( radians( seller.latitude ) ) ) ),1))
ELSE + 0
END AS score
FROM product,
productimages,
review,
seller,
site
WHERE distance < 10
AND productimage.id=product.id
AND product.sellerid =seller.id
AND product.siteid-site.id
AND productmeta.id = product.id
AND review.productid = product.id
AND ((
seller.deliverabletype = "international")
OR (
seller.deliverabletype = "country"
AND seller.country = 'country')
OR (
seller.deliverabletype = "state"
AND seller.country = 'country')
OR
OR (
seller.deliverabletype = "city"
AND seller.country = 'city'))
GROUP BY product.id mysql sql-server
The query was not well formed. One of the closing parenthesis should be at the end.
try this.
TRUNCATE(
( 6371 * acos(
cos( radians( 23 ) ) * cos( radians( seller.latitude ) ) *
cos( radians( seller.longitude ) - radians(24) ) +
sin( radians( 24 ) ) * sin( radians( seller.latitude ) )
)
),1)
ELSE + 0
END) AS score

Having issue with Having Query in MySQL

I'm trying to do this query:
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations HAVING distance < 10 AND HAVING category='%Family%'
But I get this error:
#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 'HAVING category='%Family%' LIMIT 0, 30' at line 1
Does anybody know what is the problem?
I hope category is column in your table
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations where category like '%Family%' HAVING distance < 10
You can not add having conditions twice .You can use "AND" inside the "Having" condition.So the code will be :
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations HAVING distance < 10 AND category='%Family%'
You need to remove the second HAVING and just use AND to tell MySQL that both conditions must hold.
SELECT *, ( 6371 * acos( cos( radians(43.656906) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-79.434356) ) + sin( radians(43.656906) ) * sin( radians( latitude ) ) ) ) AS distance FROM Locations HAVING distance < 10 AND category='%Family%'