2014-01-21 29 views
0

我试图执行这个查询的SQL查询,但其示值误差无法执行超过

select 
    order_id, 
    product_id 
from (select 
     order_id, 
     product_id, 
     count(*) over(partition by order_id, product_id) as dc 
     from order_items 
    ) 
where dc > 1 ; 

错误我得到是

您的SQL语法错误;检查对应于您的MySQL服务器版本的手册,以便在'(partition by order_id,product_id)'作为来自order_items的dc时使用正确的语法。其中,第1行的dc> 1'

+0

http://forums.mysql.com/read.php?32,225340,225340#msg-225340 – Barmar

+0

我上面的查询任何解决方案?请帮助我,现在我处于悲惨的状态w.r.t此查询 – obama

+0

在我发布的链接上有解决方法。 – Barmar

回答

2

不幸的是MySQL仍然没有支持开窗功能。因此OVER子句在MySQL中不起作用。

您可以使用子查询或使用用户(会话)变量来模拟它。从order_items在相同的product_id多次发生每份订单更多选择实际

一种方式

select i.*, 
     (
     select count(*) 
      from order_items 
      where order_id = i.order_id 
      and product_id = i.product_id 
     ) as dc 
    from order_items i 
having dc > 1 

或加入

select i.*, dc 
    from 
(
    select order_id, product_id, count(*) dc 
    from order_items 
    group by order_id, product_id 
    having dc > 1 
) q join order_items i 
    on q.order_id = i.order_id 
    and q.product_id = i.product_id; 

如果在另一方面,你需要只有 order_id和product_id以及此类行的总计数

select order_id, product_id, count(*) dc 
    from order_items 
group by order_id, product_id 
having dc > 1; 

这里是SQLFiddle演示

+0

好吧所以这个 – obama

+0

的任何替代品请尝试我的回答 – DhruvJoshi

+0

谢谢先生。今天我学习新事物 – obama

0
select 
     order_id, 
     product_id 
       from order_items 
     group by order_id, product_id 
     having count(*)>1