sqlserver 和MySQL的一些函式的區別

2022-03-21 20:37:13 字數 1687 閱讀 2442

相同的表資料在sqlserver和mysql中使用各自的函式得到相同的結果,如下表

drop table tb;

create table tb(id int, value1 varchar(10), value2 varchar(20));

insert into tb values(1,'aa','11');

insert into tb values(1,'bb','22');

insert into tb values(2,'aaa','111');

insert into tb values(2,'bbb','222');

insert into tb values(2,'ccc','333');

要想查詢出如下結果

在sqlserver中需要以下語句查詢

1 select

id,stuff(

(select ',' + value1 from tb as t where t.id = tb.id for xml path ('')

), 1, 1, ''

) as value1,

stuff(

(select ',' + value2 from tb as t where t.id = tb.id for xml path ('')

), 1, 1, ''

) as value2

from tb group by id

在mysql中需要以下**查詢

1 select id, group_concat(value1)value1,group_concat(value2)value2 from tb group by id;
這裡不是說mysql比sqlserver好用,只是總結相同的資料在兩個資料庫中使用不同的方法函式得到相同的結果。

例如如下sql

-- 得到兩個日期之間相差的月份數,也可以得到相差天數,只需要替換函式中第乙個引數,注意各函式引數的取值範圍,有興趣的試下其他的。

1select datediff(m,'

20160506

','20170606

');--sqlserver

2select timestampdiff(month,'

20160506

','20170606

');-- mysql

--第二個引數是否包含第乙個引數,如果包含返回1,不包含返回0

1select charindex('

a','

abcd

');--sqlserver

2select locate('

a','

abcd

');-- mysql

還有sqlserver 的substring和mysql的substr

1

create

table test2 as

select

*from test1;--

mysql

2select

*into test2 from test1;--

sqlserver

SQL Server 和MySQL的區別

sql server和mysql沒多大區別兩者資料庫都能夠在.net或j2ee下執行正常,同樣,都能夠利用raid。區別就是 有一些 控制 名字不一樣,但功能大都一樣。sql server mysql 商業資料庫,貴 不全是免費,但很便宜 只支援windows系統 支援windows和linux,u...

sql server 和mysql 建立索引

1.新增primary key 主鍵索引 alter table table name add primary key column 2.新增unique 唯一索引 alter table table name add unique column 3.新增index 普通索引 alter table...

mysql和sqlserver資料轉換

使用mysql migration toolkit,mysql官方自帶的把mssql轉換為mysql 一直下一步下一步,使用advances功能 1 jdbc jtds sqlserver localhost 1433 db cityinfo user sa password 654123 char...