我選擇兩個不同的列(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 !=''";
「explain」對您的查詢顯示了什麼內容? – 2014-11-21 08:20:07
那麼,你覺得它會讓你的頁面變慢,還是實際上頁面變慢?衡量每個查詢本身並提供有關運行時間的信息,使用EXPLAIN獲取有關MySQL如何運行查詢的適當信息。 – MatsLindh 2014-11-21 11:17:32