2014-11-21 65 views
0

我選擇兩個不同的列(count_group和group_total)具有相同的表和條件。 我覺得這種方法會讓頁面變慢。任何建議?Mysql在不同的表中選擇兩列如下

$sql3 = "SELECT SUM(mr.payment_amount) as total , 
COUNT(mr.order_id) as count_personal , 

(SELECT COUNT(DISTINCT mr.group_order_id) 
FROM event_mgmt_registration mr 
WHERE mr.event_nid = ".$nid." 
AND mr.status='completed' 
AND DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= '".$Sum['latest_date']."' 
AND mr.group_order_id != '') as count_group , 

(SELECT SUM(mr.payment_amount) 
FROM event_mgmt_registration mr 
WHERE mr.event_nid = ".$nid." 
AND mr.status='completed' 
AND DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= '".$Sum['latest_date']."' 
AND mr.group_order_id != '') as group_total 

FROM event_mgmt_registration mr 
WHERE mr.event_nid =".$nid." 
AND mr.status='completed' 
AND DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= '".$Sum['latest_date']."' 
AND mr.order_id !=''"; 
+1

「explain」對您的查詢顯示了什麼內容? – 2014-11-21 08:20:07

+0

那麼,你覺得它會讓你的頁面變慢,還是實際上頁面變慢?衡量每個查詢本身並提供有關運行時間的信息,使用EXPLAIN獲取有關MySQL如何運行查詢的適當信息。 – MatsLindh 2014-11-21 11:17:32

回答

0

爲了獲得更好的性能你最好做一個UNION拿到三個結果作爲各行:

SELECT 
SUM(mr.payment_amount) as total , 
COUNT(mr.order_id) as count_personal 
FROM event_mgmt_registration mr 
WHERE mr.event_nid =".$nid." 
AND mr.status='completed' 
AND DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= '".$molpaySum['latest_date']."' 
AND mr.order_id !='' 

UNION ALL 

(SELECT NULL, COUNT(DISTINCT mr.group_order_id) 
FROM event_mgmt_registration mr 
WHERE mr.event_nid = ".$nid." 
AND mr.status='completed' 
AND DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= '".$molpaySum['latest_date']."' 
AND mr.group_order_id != '') 

UNION ALL 

(SELECT NULL, SUM(mr.payment_amount) 
FROM event_mgmt_registration mr 
WHERE mr.event_nid = ".$nid." 
AND mr.status='completed' 
AND DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= '".$Sum['latest_date']."' 
AND mr.group_order_id != '') 
+0

$ query3 = db_query($ sql3); $ Sum = $ query3-> fetchAssoc(); $ result = $ howeiSum ['count_group'];錯誤:未定義的索引:count_group,抱歉是無法定義count_group和group_total的聯合問題?總數和count_personal很好 – 2014-11-21 08:35:04

+0

試試我的更新回答。 – Benvorth 2014-11-21 08:45:43

+0

我意識到聯盟只使用第一個選擇列名稱,所以group_order_id和payment_amount是總計和count_personal ...感謝聯合方法 – 2014-11-21 08:57:42

0

爲什麼你需要爲每個PARAM查詢?您可以使用一個查詢讓我覺得在group_total使用$Sum['latest_date']但不$molpaySum['latest_date']

試試這個

$sql3 = " 
    SELECT 
     SUM(mr.payment_amount) as total , 
     COUNT(mr.order_id) as count_personal , 
     COUNT(DISTINCT mr.group_order_id) as count_group, 
     SUM(mr.payment_amount) as group_total 

    FROM event_mgmt_registration mr 

    WHERE 
     mr.event_nid =".$nid." AND 
     mr.status='completed' AND 
     DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= 
      '".$molpaySum['latest_date']."' AND 
     mr.order_id !='' 
"; 

注意。如果它不是印刷錯誤,您應該保留此查詢,如下所示:

$sql3 = " 
    SELECT 
     SUM(mr.payment_amount) as total , 
     COUNT(mr.order_id) as count_personal , 
     COUNT(DISTINCT mr.group_order_id) as count_group, 
     (SELECT SUM(mr.payment_amount) 
     FROM event_mgmt_registration mr 
     WHERE mr.event_nid = ".$nid." 
      AND mr.status='completed' 
      AND DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= 
       '".$Sum['latest_date']."' 
      AND mr.group_order_id != '' 
     ) as group_total 

    FROM event_mgmt_registration mr 

    WHERE 
     mr.event_nid =".$nid." AND 
     mr.status='completed' AND 
     DATE_FORMAT(FROM_UNIXTIME(`datestamp`), '%Y-%m-%d') <= 
      '".$molpaySum['latest_date']."' AND 
     mr.order_id !='' 
"; 
相關問題