我正在尝试在mysql中重新创建在MS SQL中创建的东西。我有一点时间让语法正确。有谁知道对于以下内容,等效的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 :(得分: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 Fiddle。
答案 1 :(得分:0)
以上sql查询的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;