Solved

Can not convert a mssql query to a mysql query

Posted on 2010-08-20
5
330 Views
Last Modified: 2012-05-10
Hi I am converting a asp website on a windows server to php on a linux and am having trouble converting one of the mssqls to mysql.

Here is the query as a mssql:

SELECT DISTINCT(v2_dealers.ID), name, town, County, CAST(SQRT(SQUARE(" & x & " - ISNULL(tbl_Postcodes.xCoord, 2000000)) + SQUARE(" & y & " - ISNULL(tbl_Postcodes.yCoord, 200000))) / 1000 * 0.62 AS Int) AS Dist " & _
                  "FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC "& _
                  "left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make " & _
                  "WHERE v2_dealers.status='OK' and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='"&thissite&"') " & _
                  "and tbl_rvlibrary.rvid='" & rvID & "' " & _
                  "AND (SQRT(SQUARE(" & x & "- ISNULL(tbl_Postcodes.xCoord, 2000000)) + SQUARE(" & y & " - ISNULL(tbl_Postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)" & _
                  " ORDER BY Dist

And my attempt of the mysql:

SELECT DISTINCT(v2_dealers.ID), name, town, County, CAST(SQUARE('$x' IS NULL(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' IS NULL(tbl_postcodes.yCoord, 200000)) / 1000 * 0.62 AS Int) AS Dist
                  FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC
                  left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make
                  WHERE v2_dealers.status='OK' and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='$thissite')
                  and tbl_rvlibrary.rvid='$rvID'
                  AND (SQRT(SQUARE('$x' IS NULL(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' IS NULL(tbl_postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)
                  ORDER BY Dist


This is the error I receive:

#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 'IS NULL(tbl_postcodes.xCoord, 2000000)) + SQUARE(792660 - IS NULL(tbl_postcodes.' at line 1

I have tried using IF NULL but still receive the same error.
0
Comment
Question by:cyberswannie
[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
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 33483737
in MySQL, the function IsNull is not doing the same thing as in MS SQL Server.
use COALESCE instead

SELECT DISTINCT(v2_dealers.ID), name, town, County, CAST(SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y'   - COALESCE(tbl_postcodes.yCoord, 200000)) / 1000 * 0.62 AS Int) AS Dist
                  FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC
                  left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make
                  WHERE v2_dealers.status='OK' and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='$thissite')
                  and tbl_rvlibrary.rvid='$rvID'
                  AND (SQRT(SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' - COALESCE(tbl_postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)
                  ORDER BY Dist

Open in new window

0
 

Author Comment

by:cyberswannie
ID: 33483803
Thanks AngelIII, 1 for the quick reply and 2 for sorting the IS NULL problem, Unfortuneatly now I receice 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 'Int) AS Dist FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_d' at line 1

Thanks again for the help, I have never even seen the function COALESCE.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 33483927
it must be INTEGER and not INT:
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast
SELECT DISTINCT(v2_dealers.ID), name, town, County
, CAST( SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000))  
        + SQUARE('$y'   - COALESCE(tbl_postcodes.yCoord, 200000)) / 1000 * 0.62 AS Integer) AS Dist

FROM v2_dealers 
INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer 
LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC
left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make

WHERE v2_dealers.status='OK' 
  and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='$thissite')
  and tbl_rvlibrary.rvid='$rvID'
  AND (SQRT(SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' - COALESCE(tbl_postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)
ORDER BY Dist

Open in new window

0
 

Author Closing Comment

by:cyberswannie
ID: 33483993
Quick and correct couldn't of asked for any more.
0
 

Author Comment

by:cyberswannie
ID: 33483996
Thanks Angellll
0

Featured Post

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 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