将查询从SQL Server转换为MySQL

时间:2013-04-05 14:51:08

标签: mysql sql-server

我正在将项目数据库从SQL Server转换为MySQL,数据库转换已经完成。  但是在执行某些查询时,我遇到了一些错误。

我正在使用ColdFusion 8,MySQL 5.

以下是一个例子:

SELECT i.ID, s.Name, i.OccurredOn, i.MinorProblemID, 
  i.MajorProblemID, i.Problem, i.Motivation, i.Strategy1, 
  i.Strategy2, i.Strategy3, i.Decision, 
  COALESCE(CAST(s.Grade as int),-5) as Grade, i.LocationID, 
  l.Display as StartMonth, l.OrderBy as StartMonthOrder, 
  i.StudentID, u.ID as SourceID, 
  replace(u.FirstName+' '+u.LastName,'''','`') as Source,
  replace(replace(replace(replace(replace(replace(replace( 
      ltrim(replace(CAST(OccurredOn as varchar(100)),CAST(OccurredOn 
      as varchar(12)),'')),':1',':0'),':2',':0'),':3',':0'),':4',':0'),
      ':5',':0'),':05',':00'),':00',':00 ') as StartHour 
FROM ViewIncidents i 
JOIN Students s ON i.StudentID = s.ID 
LEFT OUTER JOIN Users u ON i.UserID = u.ID 
LEFT OUTER JOIN Lookup l ON l.LookupCode = 'SchoolMonths' 
  AND l.Value = CAST(OccurredOn as varchar(3)) 
WHERE i.OccurredOn > '7/1/2012' 
AND i.OccurredOn >= '7/1/2012' 
ORDER BY i.OccurredOn DESC

它在SQL Server中运行良好但在MySQL中失败:

  

执行数据库查询时出错。
您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'int)附近使用正确的语法, - 5)作为Grade,i.LocationID,l。显示为StartMonth,l.OrderBy作为Star'在第2行

请帮助我将db从mssql迁移到mysql,以及更改查询的最简单方法。

谢谢,

Prathamesh

1 个答案:

答案 0 :(得分:2)

使用CONCAT()

完成mysql中的连接
replace(CONCAT(u.FirstName, ' ', u.LastName), '''', '`') AS Source

在投射时代替INT使用SIGNED或UNSIGNED

COALESCE(CAST(s.Grade AS SIGNED), - 5) AS Grade,

而不是VARCHAR使用CHAR

CAST(OccurredOn AS CHAR(100))
除了演员和连接之外,所有其他人都很好。