最近公司项目需要从SQL Server转到MySQL, 在转的过程中遇到两者语法之间的一些差异,在网上找了解决方案后,特记录在此。由于解决方案可能有很多种,我只记录了自己使用过的,仅作参考。

1. 拼接字符串

使用group_concat方法

  • MSSQL

    ( SELECT    LEFT(dal.DeliveryAreaList,
    LEN(dal.DeliveryAreaList) - 1)
    FROM ( SELECT ( SELECT CAST(DeliveryArea AS VARCHAR)
    + '|'
    FROM Rel_MainCommodityDeliveryArea
    WHERE MainCommodityId = a.MainCommodityId
    AND DeliveryArea <> 0
    AND Disabled = 0
    ORDER BY DeliveryArea ASC
    FOR
    XML PATH('')
    ) AS DeliveryAreaList
    ) dal
    ) AS DeliveryAreasList
  • MySQL

    (select
    group_concat(rmcda.DeliveryArea order by rmcda.DeliveryArea desc separator '|')
    from Rel_MainCommodityDeliveryArea rmcda
    where rmcda.MainCommodityId = a.MainCommodityId and rmcda.DeliveryArea <> 0 and rmcda.Disabled = 0) as DeliveryAreasList

2. MySQL中和update set select语法

MySQL中update set selectfrom,需要使用更新多表的语法

  • MSSQL

    update fc
    set fc.UseScenarios = (ISNULL(fc.InheritName, '')
    + ISNULL(fmc.MainCommodityName_Postfix, '')
    + '-' + ISNULL(cn.ChannelAlias, '')
    + ISNULL(fc.CommodityName_Postfix, '')),
    fc.UseScenariosEn = (ISNULL(fc.CommodityName_Prefix, '')
    + ISNULL(fc.InheritName, '')
    + ISNULL(fmc.MainCommodityName_Postfix,
    '')
    + ISNULL(fc.CommodityName_Postfix, '')),
    fc.[Rec_ModifyBy] = '{updateUser}',
    fc.[Rec_ModifyTime] = now(3)
    from Fct_Commodity as fc
    inner join Fct_MainCommodity as fmc on fc.MainCommodityId = fmc.MainCommodityId
    inner join Dim_Channel as cn on fc.ChannelId = cn.ChannelId
    where fc.Disabled = 0
    and fmc.Disabled = 0
    and fc.InheritName is not null
    and fc.InheritName <> ''
    and fmc.[MainCommodityCode] in ({codeList})
  • MySQL

    update Fct_Commodity fc, Fct_MainCommodity fmc, Dim_Channel cn
    set fc.UseScenarios = (ifnull(fc.InheritName, '')
    + ifnull(fmc.MainCommodityName_Postfix, '')
    + '-' + ifnull(cn.ChannelAlias, '')
    + ifnull(fc.CommodityName_Postfix, '')),
    fc.UseScenariosEn = (ifnull(fc.CommodityName_Prefix, '')
    + ifnull(fc.InheritName, '')
    + ifnull(fmc.MainCommodityName_Postfix,
    '')
    + ifnull(fc.CommodityName_Postfix, '')),
    fc.Rec_ModifyBy = '{updateUser}',
    fc.Rec_ModifyTime = now(3)
    where
    fc.MainCommodityId = fmc.MainCommodityId
    and fc.ChannelId = cn.ChannelId
    and fc.Disabled = 0
    and fmc.Disabled = 0
    and fc.InheritName is not null
    and fc.InheritName <> ''
    and fmc.MainCommodityCode in ({codeList})

3. MySQL子查询中使用limit

MySQL中子某些子查询不允许limit, 如需要使用,需要用select再包一层

  • MSSQL

    SELECT UnitId,UnitName
    FROM Dim_Unit
    WHERE UnitName IN (
    SELECT TOP 6 fmc.Unit
    FROM Fct_MainCommodity fmc INNER JOIN Dim_Unit du ON fmc.Unit=du.UnitName
    WHERE fmc.Disabled=0 AND du.Disabled=0
    GROUP BY fmc.Unit
    ORDER BY COUNT(fmc.Unit) DESC
    )
  • MySQL

    select
    UnitId,
    UnitName
    from Dim_Unit
    where UnitName in (
    select temp.Unit
    from
    (select fmc.Unit
    from Fct_MainCommodity fmc inner join Dim_Unit du on fmc.Unit = du.UnitName
    where fmc.Disabled = 0 and du.Disabled = 0
    group by fmc.Unit
    order by COUNT(fmc.Unit) desc
    limit 6) temp)

4. Parameter '@Rec_CreateTime' must be defined

参数化拼sql, 不要用now(3), 直接在代码里面获取当前时间

  • MSSQL

    public static Hashtable CreateByCheck(Hashtable htValue,string userID)
    {
    if (!htValue.Contains("Rec_CreateTime"))
    {
    htValue.Add("Rec_CreateTime", "now(3)");
    }
    if (!htValue.Contains("Rec_CreateBy"))
    {
    htValue.Add("Rec_CreateBy", HttpContext.Current == null ? "admin" : userID);
    }
    return htValue;
    }
  • MySQL

    public static Hashtable CreateByCheck(Hashtable htValue,string userID)
    {
    if (!htValue.Contains("Rec_CreateTime"))
    {
    htValue.Add("Rec_CreateTime", DateTime.Now);
    }
    if (!htValue.Contains("Rec_CreateBy"))
    {
    htValue.Add("Rec_CreateBy", HttpContext.Current == null ? "admin" : userID);
    }
    return htValue;
    }

5 拼接字符串+字符集

  1. (MainCommodityName + ifnull(MainCommodityName_Postfix, ''))拼接得不到想要的结果

  2. [HY000][1267] Illegal mix of collations (utf8_bin,NONE) and (utf8_general_ci,COERCIBLE) for operation '=': 需要加 collate utf8_general_ci 统一字符集

  • MSSQL

    select MainCommodityName
    from Fct_MainCommodity
    where (MainCommodityName + ifnull(MainCommodityName_Postfix, '')) = '附件上传原料A进A出1003' and Disabled = 0 and
    ifnull(IsAutoHide, 0) != 1 and MainCommodityId != '27135417-a42b-453f-a1cc-1617d6fc471e';
  • MySQL

    select MainCommodityName
    from Fct_MainCommodity
    where CONCAT(MainCommodityName, cast(ifnull(MainCommodityName_Postfix, '') as nchar(50))) collate utf8_general_ci =
    '附件上传原料A进A出1003'
    and Disabled = 0 and ifnull(IsAutoHide, 0) != 1 and MainCommodityId != '27135417-a42b-453f-a1cc-1617d6fc471e';

6 SQL中使用正则

MSSQL中LIKE后面可以使用正则,但是MYSQL需要使用REGEXP

  • MSSQL
 select isnull( MAX(BrandCode),99999)+1 as BrandCode from Fct_Brand
where BrandCode like '[0-9][0-9][0-9][0-9][0-9][0-9]'
  • MySQL
select ifnull( MAX(BrandCode),99999)+1 as BrandCode from Fct_Brand
where BrandCode regexp '[0-9][0-9][0-9][0-9][0-9][0-9]'

转MySQL遇到的语法差异及解决方案的更多相关文章

  1. SQLServer、MySQL、Oracle语法差异小集锦

    一.差异集锦 在建表的时候,只有自增的语法不同. 下面给出3种数据库通用的建表与初始化测试语句: CREATE TABLE Country( Id int PRIMARY KEY, Name ) ); ...

  2. mybatis中mysql和oracle的差异

    1.applicationContext.xml中的配置差异: 在applicationContext.xml的数据源dataSource的配置中,mysql数据库需要心跳包的配置,而oracle中不 ...

  3. Kotlin VS Java:基本语法差异

    Kotlin比Java更年轻,但它是一个非常有前途的编程语言,它的社区不断增长. 每个人都在谈论它,并说它很酷. 但为什么这么特别? 我们准备了一系列文章,分享我们在Kotlin开发Android应用 ...

  4. 【优化】MySQL千万级大表优化解决方案

    问题概述 使用阿里云rds for MySQL数据库(就是MySQL5.6版本),有个用户上网记录表6个月的数据量近2000万,保留最近一年的数据量达到4000万,查询速度极慢,日常卡死.严重影响业务 ...

  5. MySQL创建索引语法

    1.介绍: 所有mysql索引列类型都可以被索引,对来相关类使用索引可以提高select查询性能,根据mysql索引数,可以是最大索引与最小索引,每种存储引擎对每个表的至少支持16的索引.总索引长度为 ...

  6. MYSQL整理的语法

    MYSQL整理的语法 http://www.cnblogs.com/suoning/p/5744849.html

  7. Sqlite基础及其与SQLServer语法差异

    1 TOP 这是一个大家经常问到的问题,例如在SQLSERVER中可以使用如下语句来取得记录集中的前十条记录: SELECT TOP 10 * FROM [index] ORDER BY indexi ...

  8. MySQL的一些语法总结

    初学MySQL,今天遇到了一个问题,然后汇总了一下MySQL的一些语法 1. date和datetime类型是不同的 date只记录日期(包括年月日),datetime记录日期和时间(包括年月日时分秒 ...

  9. sql点滴41—mysql常见sql语法

    原文:sql点滴41-mysql常见sql语法 ALTER TABLE:添加,修改,删除表的列,约束等表的定义. 查看列:desc 表名; 修改表名:alter table t_book rename ...

随机推荐

  1. [Robot Framework] 如何在Setup中用Run Keywords执行多个带参数的关键字

    参考文档:http://www.howtobuildsoftware.com/index.php/how-do/bZ7q/robotframework-setup-teardown-robot-fra ...

  2. SQL0668N 不允许对表"xxx"执行操作,原因码为 "1"

    使用db2 load导入30万条记录到某个表,成功后发现表被锁了,并显示: SQL0668N  不允许对表"xxx"执行操作,原因码为 "1" google了一 ...

  3. “matplotlib display text must have all code points < 128 or use Unicode strings”解决方法

    import sys reload(sys) sys.setdefaultencoding('utf-8') 插入以上代码,便可解决.

  4. 20 模块之 re subprocess

    re: 什么是正则: 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 ...

  5. Anaconda 3中配置OpenCV

    平台:win10 x64+Anaconda 3(64-bit)+opencv_python-3.4.5+contrib-cp37-cp37m-win_amd64 一.OpenCV下载 Python环境 ...

  6. Codeforces 1111 简要题解

    文章目录 A题 B题 C题 D题 E题 传送门 A题 传送门 题意简述:把262626个英文字母分成两类A,BA,BA,B,AAA类字符可以转成AAA类字符,BBB类字符可以转成BBB类字符,问给出的 ...

  7. ODT(old driver tree)详解(带例题)

    文章目录 ODT简介 实现前提&&实现原理 初始化 split操作 assign操作 其它操作 区间第k小 区间加 区间所有数的k次方和 几道水题 ODT简介 ODT(old driv ...

  8. react父转子

    父组件使用子组件,子组件绑定父组件数据 ,子组件用props使用父组件数据 import React, { Component } from 'react'; import logo from './ ...

  9. abaqus学习笔记-abaqus与umat调用基本原理

    参考: 1.http://ivt-abaqusdoc.ivt.ntnu.no:2080/v6.14/books/sub/default.htm 2.ABAQUS 用户材料子程序开发及应用-杨曼娟 3. ...

  10. Ng第十八课:应用实例:图片文字识别(Application Example: Photo OCR)

    18.1  问题描述和流程图 18.2  滑动窗口 18.3  获取大量数据和人工数据 18.4  上限分析:哪部分管道的接下去做 18.1  问题描述和流程图 图像文字识别应用所作的事是,从一张给定 ...