2014-12-06 36 views
0

我想在mysql中重新創建一些在MS SQL中創建的東西。我有一段時間讓語法正確。有誰知道什麼等效的MySQL查詢將爲以下內容:sql-server到mysql的翻譯

create table #tmp 
(id int, Ran varchar(10), Result int, ref_id int) 
insert #tmp values (1, 'Object1', 4.0, 1) 
insert #tmp values (2, 'Object2', 100, 1) 
insert #tmp values (3, 'Object1', 6.0, 2) 
insert #tmp values (4, 'Object3', 89.0, 2) 

select * from #tmp 

Select t.ref_id 
     ,TK = max(case when t.Ran ='Object1' then t.[Result] end) 
     ,CRP= max(case when t.Ran ='Object2' then t.[Result] end) 
     ,HPT= max(case when t.Ran = 'Object3' then t.[Result] end) 
     From #tmp t 
group by t.ref_id 

謝謝你看一看!

+0

什麼是你得到錯誤信息? – dpw 2014-12-06 01:09:06

回答

2

這似乎並不難:

create temporary table tmp (
    id int, 
    Ran varchar(10), 
    Result int, 
    ref_id int 
); 

insert into tmp(id, Ran, Result, ref_id) values (1, 'Object1', 4.0, 1); 
insert into tmp(id, Ran, Result, ref_id) values (2, 'Object2', 100, 1); 
insert into tmp(id, Ran, Result, ref_id) values (3, 'Object1', 6.0, 2); 
insert into tmp(id, Ran, Result, ref_id) values (4, 'Object3', 89.0, 2); 

select * from tmp; 

Select t.ref_id, 
     max(case when t.Ran ='Object1' then t.Result end) as TK, 
     max(case when t.Ran ='Object2' then t.Result end) as CRP, 
     max(case when t.Ran = 'Object3' then t.Result end) as HPT 
From tmp t 
group by t.ref_id; 

Here是一個非常接近SQL小提琴。上面的SQL查詢

0

MySQL的等效查詢:

create table #tmp 
(id int, Ran varchar(10), Result int, ref_id int); 
insert into #tmp values (1, 'Object1', 4.0, 1); 
insert into #tmp values (2, 'Object2', 100, 1); 
insert into #tmp values (3, 'Object1', 6.0, 2); 
insert into #tmp values (4, 'Object3', 89.0, 2); 

select * from #tmp; 

Select t.ref_id 
     ,TK = max(case when t.Ran ='Object1' then t.Result end) 
     ,CRP= max(case when t.Ran ='Object2' then t.Result end) 
     ,HPT= max(case when t.Ran ='Object3' then t.Result end) 
     from #tmp t 
group by t.ref_id;