2014-06-05 73 views
3

好吧,我有以下幾點:得到兩列在MySQL和繁殖的結果

user id meta 
    2  _qty  3 
    2  Weight 20 
    1  _qty  1 
    1  weight 30 

我需要獲取用戶ID欄然後由權重賦予我的用戶ID 2 = 60和用戶ID乘以數量= 1 30 ...... 我試過以下,但無濟於事:

$myrows = $wpdb->get_results("SELECT meta_value, SUM(_qty*weight) AS product_id FROM {$wpdb->prefix}woocommerce_order_itemmeta GROUP BY ($query_select_order_items)"); 

其中($query_select_order_items)是USER_ID。 我該如何解決這個問題?

實際PHP我目前正在運行:

/** 
* Returns all the orders made by the user 
* 
* @param int $user_id 
* @param string $status (completed|processing|canceled|on-hold etc) 
* @return array of order ids 
*/ 
function fused_get_all_user_orders($user_id,$status='completed'){ 
    if(!$user_id) 
     return false; 

    $orders=array();//order ids 

    $args = array(
     'numberposts'  => -1, 
     'meta_key'  => '_customer_user', 
     'meta_value'  => $user_id, 
     'post_type'  => 'shop_order', 
     'post_status'  => 'publish', 
     /* 'tax_query'=>array(
       array(
        'taxonomy' =>'shop_order_status', 
        'field'  => 'slug', 
        'terms'  =>$status 
        ) 
     ) */ 
    ); 

    $posts=get_posts($args); 
    //get the post ids as order ids 
    $orders=wp_list_pluck($posts, 'ID'); 

    return $orders; 
} 
function fused_get_all_products_ordered_by_user($user_id=false){ 

$orders=fused_get_all_user_orders($user_id); 
if(empty($orders)) 
    return false; 

$order_list='('.join(',', $orders).')';//let us make a list for query 

//so we have all the orders made by this user which was successfull 

//we need to find the products in these order and make sure they are downloadable 

// find all products in these order 

global $wpdb; 
$query_select_order_items="SELECT order_item_id as id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id IN {$order_list}"; 

$query_select_product_ids="SELECT meta_value as product_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s AND order_item_id IN ($query_select_order_items)"; 


$products=$wpdb->get_col($wpdb->prepare($query_select_product_ids,'weight')); 

$qty = $wpdb->get_col($wpdb->prepare($query_select_product_ids,'_qty')); 


return $products; 
} 

凡目前到底是$產品返回重的列表,或者如果我將其設置爲_qty將返回訂購數量的列表.. 。 sqlfiddle

從草莓更新回答:

"SELECT order_item_id, weight * quantity total FROM ( 
SELECT order_item_id, 
    MAX(CASE WHEN meta_key = '_qty' THEN meta_value ELSE 0 END) quantity , 
    MAX(CASE WHEN meta_key = 'weight' THEN meta_value ELSE 0 END) weight 
    FROM wp_woocommerce_order_itemmeta GROUP BY order_item_id) 
    WHERE order_item_id IN ($query_select_order_items) x;" 
+0

嘗試此查詢' 「選擇meta_value,_qty *體重從table_name的GROUP BY」。$ query_select_order_items' – krishna

+0

這不工作... – vimes1984

+1

你確定你每個用戶只有一個'_qty'和'weight'嗎? –

回答

1

這裏有一種方法......

DROP TABLE IF EXISTS my_table; 

CREATE TABLE my_table 
(entity INT NOT NULL 
,attribute VARCHAR(12) NOT NULL 
,value INT NOT NULL 
,PRIMARY KEY(entity,attribute) 
); 

INSERT INTO my_table VALUES 
(2,'quantity',3), 
(2,'weight',20), 
(1,'quantity',1), 
(1,'weight',30); 

SELECT entity, weight * quantity total 
    FROM 
    (SELECT entity 
      , MAX(CASE WHEN attribute = 'quantity' THEN value END) quantity 
      , MAX(CASE WHEN attribute = 'weight' THEN value END) weight 
     FROM my_table 
     GROUP 
      BY entity 
    ) x; 

+--------+-------+ 
| entity | total | 
+--------+-------+ 
|  1 | 30 | 
|  2 | 60 | 
+--------+-------+ 

...甚至只是......

SELECT entity 
    , ROUND(EXP(SUM(LOG(value)))) total 
    FROM my_table 
GROUP 
    BY entity; 
+--------+-------+ 
| entity | total | 
+--------+-------+ 
|  1 | 30 | 
|  2 | 60 | 
+--------+-------+ 

適應第一查詢到你的提琴可能是這樣的......

SELECT order_item_id, weight * quantity total 
    FROM 
    (SELECT order_item_id 
      , MAX(CASE WHEN meta_key = '_qty' THEN meta_value ELSE 0 END) quantity 
      , MAX(CASE WHEN meta_key = 'weight' THEN meta_value ELSE 0 END) weight 
     FROM wp_woocommerce_order_itemmeta 
     GROUP 
      BY order_item_id 
    ) x; 

...第二個可能看起來像這樣...

SELECT order_item_id 
    , ROUND(EXP(SUM(LOG(meta_value)))) total 
    FROM wp_woocommerce_order_itemmeta 
WHERE meta_key IN('_qty','weight') 
GROUP 
    BY order_item_id; 

...和適應率先您的最新要求...

SELECT i.order_item_id, weight * quantity total 
    FROM 
    (SELECT order_item_id 
      , MAX(CASE WHEN meta_key = '_qty' THEN meta_value ELSE 0 END) quantity 
      , MAX(CASE WHEN meta_key = 'weight' THEN meta_value ELSE 0 END) weight 
     FROM wp_woocommerce_order_itemmeta 
     GROUP 
      BY order_item_id 
    ) x 
    JOIN wp_woocommerce_order_items i 
    ON i.order_item_id = x.order_item_id 
WHERE i.order_id IN(647,649,650); 
+0

這種類型的先生是天才謝謝你..... 你有什麼想法我會如何將它結合到它:「SELECT order_item_id作爲id從{$ wpdb->前綴} woocommerce_order_items WHERE order_id IN {$ order_list} 「 其中order_list是我需要的ID實際訂單... – vimes1984

+0

HAhahaha確定它只是我一直在這一整個上午奮鬥,而我在我的最後... – vimes1984

+0

這是什麼結束X爲? – vimes1984

1
SELECT A.user_id, 
     SUM(A.meta_value * B.meta_value) 
FROM t A INNER JOIN t B ON A.user_id = B.user_id 
WHERE A.meta = 'weight' 
    AND B.meta = '_qty' 
GROUP BY 
     A.user_id 

,您可以嘗試在此fiddle

+0

好吧我不知道我需要在這裏取代所以它適用於我的代碼..我在哪裏輸入$ query_select_order_items – vimes1984

+0

@ vimes1984我不明白你的評論。您可以從小提琴中看到查詢返回的內容。我想你只需要將表名從t更改爲yourTableName,然後在php中獲取結果以獲取user_id和計算的字段值。不是嗎? – kiks73

1

您可以使用此查詢:

select tbl1.val*tbl2.val 
from mytbl as tbl1,mytbl as tbl2 
where tbl1.user_id=tbl2.user_id 
and tbl2.meta='Weight' 
and tbl1.meta='_qty' 

而且我準備fiddle of it

+0

我還在我的實際表格中添加了一個提琴... http://sqlfiddle.com/#!2/10138 – vimes1984

+0

@ vimes1984哪一個是您想要的表格來執行操作? –

+0

wp_woocommerce_order_itemmeta – vimes1984