将 Access SQL 转换为 MySQL

Posted

技术标签:

【中文标题】将 Access SQL 转换为 MySQL【英文标题】:Converting Access SQL to MySQL 【发布时间】:2013-03-26 04:25:38 【问题描述】:

您好,我正在尝试将一些 Access SQL 查询转换为 mysql 查询,但由于我不确定如何转换其中的一些代码,因此遇到了麻烦。请有人帮忙。

1.

SELECT Notices.Promoter, 
Sum(IIf([Notices].[Notice Type]='GRANT PERMIT' Or [Notices].[Notice Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) AS Granted, 
Sum(IIf([Notices].[Notice Type]='REFUSE APPLICATION',1,0)) AS Refused, 
Sum(IIf([Notices].[Permit Status]='Deemed',1,0)) AS Deemed
FROM Notices
GROUP BY Notices.Promoter;

2.

SELECT [Notices].Promoter, 
Sum(IIf([Notices].[Notice Type]='VARIATION',1,0)) AS Variation, 
Sum(IIf([Notices].[Notice Type]='TWO HOURS AFTER',1,0)) AS [Two Hours After],
Sum(IIf([Notices].[Notice Type]='THREE MONTHS',1,0)) AS [Three Months],
Sum(IIf([Notices].[Notice Type]='THREE DAY',1,0)) AS [Three Day], 
Sum(IIf([Notices].[Notice Type]='TEN DAY',1,0)) AS [Ten Day]
FROM Notices
GROUP BY [Notices].Promoter;

Access 数据库和 MySQL 数据库标记相同,我只需要覆盖这两个数据库,以便我得到相同的结果。

【问题讨论】:

【参考方案1】:

访问转义关键字使用[],但是mysql使用``,mysql可以使用CASE替换IIf。

SELECT `Notices`.Promoter, 
Sum(CASE WHEN `Notices`.`Notice Type`=`VARIATION` THEN 1 ELSE 0 END) AS Variation, 
Sum(CASE WHEN `Notices`.`Notice Type`='TWO HOURS AFTER' THEN 1 ELSE 0 END) AS `Two Hours    After`,
Sum(CASE WHEN `Notices`.`Notice Type`='THREE MONTHS' THEN 1 ELSE 0 END) AS `Three Months`,
Sum(CASE WHEN `Notices`.`Notice Type`='THREE DAY' THEN 1 ELSE 0 END) AS `Three Day`, 
Sum(CASE WHEN `Notices`.`Notice Type`='TEN DAY' THEN 1 ELSE 0 END) AS `Ten Day`
FROM Notices
GROUP BY `Notices`.Promoter;

【讨论】:

以上是关于将 Access SQL 转换为 MySQL的主要内容,如果未能解决你的问题,请参考以下文章

将 SQL 转换为 ACCESS

将 Access 查询转换为 VB.net SQL 语句

将 Access SQL 转换为 MySQL

将 Access 交叉表查询转换为 SQL Server

将查询从 SQL Server 转换为 Access 2000

将 Access 数据库转换为 SQL Server