2014-01-21 89 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