为什么在MS SQL里select convert(varchar,OrderID)+convert(varchar,orderdate,120) from dbo.Orders 
这样可以得到 123452009-06-20 12:00:00 这样的字符而在MYSQL里变成了 2010 ??我想要ID+datetime,转成string(从而成唯一值)还有,怎样才能达到下面的效果?  没办法,只对MSSQL熟悉,不熟悉MYSQLdeclare sd datetime;
set sd = select max(ADatetime) from tableA;

解决方案 »

  1.   

    MySQL 与 MS SQL Server 在函数上有些不同。如下即可mysql> desc tx;
    +-----------+---------+------+-----+---------+-------+
    | Field     | Type    | Null | Key | Default | Extra |
    +-----------+---------+------+-----+---------+-------+
    | OrderID   | int(11) | YES  |     | NULL    |       |
    | orderdate | date    | YES  |     | NULL    |       |
    +-----------+---------+------+-----+---------+-------+
    2 rows in set (0.06 sec)mysql>
    mysql> select * from tx;
    +---------+------------+
    | OrderID | orderdate  |
    +---------+------------+
    |   12345 | 2009-06-20 |
    +---------+------------+
    1 row in set (0.00 sec)mysql>
    mysql> select OrderID,orderdate,
        ->  concat(OrderID,date_format(orderdate,'%Y-%m-%d %H:%i:%s')) as skey
        -> from tx;
    +---------+------------+--------------------------+
    | OrderID | orderdate  | skey                     |
    +---------+------------+--------------------------+
    |   12345 | 2009-06-20 | 123452009-06-20 00:00:00 |
    +---------+------------+--------------------------+
    1 row in set (0.00 sec)mysql>
      

  2.   

    mysql> select max(orderdate) into @sd from tx;
    Query OK, 1 row affected (0.00 sec)mysql> select @sd;
    +------------+
    | @sd        |
    +------------+
    | 2009-06-20 |
    +------------+
    1 row in set (0.00 sec)mysql>
      

  3.   

    id 是 varchar 型吖  不是int
      

  4.   

    我自己猜的,猜错了也很正常。即使id 是char的,上面的语句也可以,无须任何修改。
      

  5.   

    不行吖
    ERROR 1267 (HY000): Illegal mix of collations (gb2312_chinese_ci,IMPLICIT) and (
    latin1_swedish_ci,COERCIBLE) for operation 'concat'
      

  6.   

    改成varchar如下,没有问题。mysql> desc tx;
    +-----------+-------------+------+-----+---------+-------+
    | Field     | Type        | Null | Key | Default | Extra |
    +-----------+-------------+------+-----+---------+-------+
    | OrderID   | varchar(10) | YES  |     | NULL    |       |
    | orderdate | date        | YES  |     | NULL    |       |
    +-----------+-------------+------+-----+---------+-------+
    2 rows in set (0.02 sec)mysql> select * from tx;
    +---------+------------+
    | OrderID | orderdate  |
    +---------+------------+
    | 12345   | 2009-06-20 |
    +---------+------------+
    1 row in set (0.00 sec)mysql> select OrderID,orderdate,
        ->  concat(OrderID,date_format(orderdate,'%Y-%m-%d %H:%i:%s')) as skey
        -> from tx;
    +---------+------------+--------------------------+
    | OrderID | orderdate  | skey                     |
    +---------+------------+--------------------------+
    | 12345   | 2009-06-20 | 123452009-06-20 00:00:00 |
    +---------+------------+--------------------------+
    1 row in set (0.00 sec)mysql>
    建议你能给出你建表create table 语句,以有测试用数据,然后你自己的SQL语句,否则很难再现你的错误,也就无从判断问题。show create table yourtable; 查看你的建表语句。
      

  7.   

    还是出现错误.我用的是MYSQL5.0create table record_tmp(
    no varchar(32) not null,
    datetimeA datetime not null,
    machineid int(11) not null)
    ENGINE=InnoDB AUTO_INCREMENT=138 DEFAULT CHARSET=gb2312;
      

  8.   

    你的SQL语句是什么?!不行你就用这句试一下:
    select no,datetimeA,
    concat(no,CONVERT(date_format(datetimeA,'%Y-%m-%d %H:%i:%s') USING gb2312 )) as skey
    from record_tmp;我用的你create table 建表测试没有任何问题
    mysql> create table record_tmp(
        -> no varchar(32) not null,
        -> datetimeA datetime not null,
        -> machineid int(11) not null)
        -> ENGINE=InnoDB AUTO_INCREMENT=138 DEFAULT CHARSET=gb2312;
    Query OK, 0 rows affected (0.39 sec)mysql> insert into record_tmp values ('12345','2009-06-20 14:34:45',1);
    Query OK, 1 row affected (0.06 sec)mysql> select * from record_tmp;
    +-------+---------------------+-----------+
    | no    | datetimeA           | machineid |
    +-------+---------------------+-----------+
    | 12345 | 2009-06-20 14:34:45 |         1 |
    +-------+---------------------+-----------+
    1 row in set (0.06 sec)mysql> select no,datetimeA,
        ->  concat(no,date_format(datetimeA,'%Y-%m-%d %H:%i:%s')) as skey
        -> from record_tmp;
    +-------+---------------------+--------------------------+
    | no    | datetimeA           | skey                     |
    +-------+---------------------+--------------------------+
    | 12345 | 2009-06-20 14:34:45 | 123452009-06-20 14:34:45 |
    +-------+---------------------+--------------------------+
    1 row in set (0.00 sec)mysql>
      

  9.   

    USING gb2312 后可以了还有一个问题,解完后,两帖的分都给你能不能这样insert into tableA (select no,datetimeA from tableB where concat(no,date_format(datetimeA,'%Y-%m-%d %H:%i:%s')) not in (select concat(no,date_format(datetimeA,'%Y-%m-%d %H:%i:%s')) from tableA) )
      

  10.   

    可以,不过你要确保 tableA  中只有两个字段,no,datetimeA,否则你要指定好字段。另外不需要select 外的那对括号。
    insert into tableA 
    select no,datetimeA from tableB 
    where concat(no,date_format(datetimeA,'%Y-%m-%d %H:%i:%s')) not in 
    (select concat(no,date_format(datetimeA,'%Y-%m-%d %H:%i:%s')) from tableA);
    不过效率上来说,不如下面的语句。
    insert into tableA
    select no,datetimeA from tableB 
    where not exists (
    select 1 from tableA 
    where no=tableB.no 
    and datetimeA=tableB.datetimeA);mysql> create table tableA(
        -> no varchar(32) not null,
        -> datetimeA datetime not null
        -> );
    Query OK, 0 rows affected (0.09 sec)mysql>
    mysql> create table tableB(
        -> no varchar(32) not null,
        -> datetimeA datetime not null
        -> );
    Query OK, 0 rows affected (0.08 sec)mysql> insert into tableA
        ->  select no,datetimeA from tableB
        ->  where concat(no,date_format(datetimeA,'%Y-%m-%d %H:%i:%s')) not in
        ->          (select concat(no,date_format(datetimeA,'%Y-%m-%d %H:%i:%s')) from tableA);
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0mysql>
      

  11.   

    结帖了,发现为什么出错了,此前你还帮我解决的那个procedure里,有点问题,12345 筛选成1了DECLARE a,a1 CHAR default '';  应该改成 DECLARE a,a1,ao CHAR(5) default '99999';可能因为1位数跟5位数不对,所以那两列合起来就提示错了,不知道是不是MYSQL的BUG还有一个是 a != a1 改成 a <> a1 了感谢ACMAIN_CHM的帮助,感恩吖!
      

  12.   

    不会啊,  a != a1 和 a  <> a1 是等同的啊。mysql> select 'a'<>'b','a'!='b';
    +----------+----------+
    | 'a'<>'b' | 'a'!='b' |
    +----------+----------+
    |        1 |        1 |
    +----------+----------+
    1 row in set (0.00 sec)mysql> select 'a'<>'a','a'!='a';
    +----------+----------+
    | 'a'<>'a' | 'a'!='a' |
    +----------+----------+
    |        0 |        0 |
    +----------+----------+
    1 row in set (0.00 sec)mysql>
      

  13.   

    DECLARE a,a1,ao CHAR default '';因为你原来的测试数据中全是一位的字符,所以用直接用CHAR了,CHAR = CHAR(1),比较的时候会和CHAR(5)的不同 'a' <> 'a     '。不算是MySQL的bug.
      

  14.   


    但是奇怪的是, 用了!=得不到正确的数据,也就是说还是1,但是用了<>后就成了12345