Solved

Can not convert a mssql query to a mysql query

Posted on 2010-08-20
5
319 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
  • 3
  • 2
5 Comments
 
LVL 142

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 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now