0

我正在尝试在我的工作台中打开一个脚本,但关键字 PIVOT 以红色下划线显示,并显示一条错误消息:` 语法错误,意外 IDENT_QUOTED。脚本:

select * from 
(select c1.id, a.num0,a.num1 from table1 c1
Inner Join
(select c2.id, if(team=1,1,0) as num0, if(team=2,1,0)as num1 from table1 c2,table2 r2
where c2.q_id = 2046 and r2.q_id = 2046 group by c2.d)a on a.id = c1.id) pvt

PIVOT(
For content
IN([team1],[team2]))pvt2
4

2 回答 2

0

您的查询有语法错误,因为 MySQL 不支持PIVOT. 你需要做一些额外的事情才能在 MySQL 中实现你想要的。请参阅相关问题。

于 2013-03-28T08:46:39.787 回答
0

MySQL 没有 PIVOT 函数。但是,可以使用带有CASE表达式的聚合函数来复制此功能。

根据您的原始查询,您似乎需要类似以下内容:

select t2.id,
  sum(case when team = 1 then 1 else 0 end) num0,
  sum(case when team = 2 then 1 else 0 end) num1
from table1 t1
inner join table2 t2
  on t1.q_id = t2.q_id
group by t2.id
于 2013-03-28T09:47:59.897 回答