Home > Documentation > SQL Server to MySQL
How to convert SQL Server queries into MySQL format |
[SQL Server to MySQL Converter] [About Migration] [Safe Types Mapping] [Stored Procedures] |
When migrating databases from SQL Server to MySQL server it is often necessary to translate MS SQL queries according to MySQL syntax as well. Syntax of SQL queries in SQL Server and MySQL are similar but not identical. This article discovers 10 most popular differences between MS SQL and MySQL syntax. The target audience for this guide should have general database management knowledge and experience in composing SQL queries.
Also, SQL Server provides effective solution to avoid naming objects conflict
and to manage user permissions on data access. This is schema, a logic
container used to group and categorize objects inside the single database.
When using schema the full name of database object in query may look like
database.schema.object
. However, there is no such semantic in MySQL,
so all schema names must be cut off from queries.
'+'
allows to concatenate strings like this:
'string1' + 'string2'
. In MySQL such expressions must be replaced by
CONCAT('string1', 'string2')
.
'TOP (100) PERCENT'
pattern just cut it off
when composing MySQL query. If there is another percentage amount in
that pattern, it can be replace by the following code in MySQL (works in
MySQL 5.0.7 and higher):
SET @amount =(SELECT COUNT(*) FROM %table name%) * %percentage% / 10; PREPARE STMT FROM '%original query% FROM %table name% LIMIT ?'; EXECUTE STMT USING @amount;
JOIN
constructions are very similar in SQL Server and MySQL.
The only difference is that MS SQL keyword WHERE
is replaced by
ON
in MySQL. For example:
... table1 CROSS JOIN table2 WHERE conditionmust be translated into
... table1 CROSS JOIN table2 ON condition
OUTER APPLY
to join
two table expressions with evaluating right-side expression
for each row from the left-side one:
SELECT c.ID, c.FirstName, c.LastName, c.PhoneNumber, p.Id, p.Debit, p.Credit FROM customers c OUTER APPLY ( SELECT p.* FROM payments p WHERE c.CustNumber = p.CustNumber ORDER BY p.id DESC ) pIt must be replaced by
LATERAL JOIN
in MySQL:
SELECT c.ID, c.FirstName, c.LastName, c.PhoneNumber, p.Id, p.Debit, p.Credit FROM customers c, LATERAL JOIN ( SELECT p.* FROM payments p WHERE c.CustNumber = p.CustNumber ORDER BY p.id DESC ) p
FOR XML PATH
syntax pattern in SQL Server:
SELECT distinct r.price, STUFF( (SELECT distinct ','+ Cast(a.code as varchar) FROM tbl_price a WHERE r.price = a.price FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '') FROM tbl_price rThose queries must be rewritten using
GROUP_CONCAT
in MySQL:
SELECT price, GROUP_CONCAT(DISTINCT code) code FROM tbl_price GROUP BY price
Have questions? Contact us