我需要在php腳本中使用複雜的查詢與連接,子查詢等來爲數據表服務器端進程生成Json。 Datatables不接受子查詢,所以我需要一個「扁平」查詢。我想過生成一個視圖,但是在MySQL視圖上不接受子查詢! 這是MySQL查詢。你會推薦什麼 ?複雜的MySQL查詢:使用視圖(數據表......)的解決方案
SELECT a.Customers
,a.Agent
,a.status_name AS Current_Status
,round(c.LTGGR - e.LT_TotBonus, 0) AS LT_NGR
,CONCAT (
round(e.LT_TotBonus/c.LTRM * 100, 0)
,'%'
) AS LT_Bonus_RM_per
,CONCAT (
'x'
,round(c.LTBETS/(c.LTRM + e.LT_TotBonus), 0)
) AS LT_Wagering
,coalesce(d.Last_30D_RM, 0) AS Last_30D_RM
,coalesce(d.Last_30D_NGR, 0) AS Last_30D_NGR
,coalesce(g.CashBack_Last30D, 0) AS Cash_Back_30D
,CONCAT (
(f.cash_back_BO_per * 100)
,'%'
) AS status_cash_Back_BO_per
,CONCAT (
f.cash_back_per * 100
,'%'
) AS cash_back_percent
,CASE
WHEN f.cash_back_per = 0
THEN 0
WHEN c.LTGGR - e.LT_TotBonus < 0
THEN 0
WHEN d.Last_30D_RM < 0
THEN 0
WHEN e.LT_TotBonus/c.LTRM * 100 >= f.cash_back_BO_per * 100
THEN 0
WHEN round(d.Last_30D_NGR * f.cash_back_per, 0) - coalesce(g.CashBack_Last30D, 0) < 0
THEN 0
WHEN isnull(d.Last_30D_NGR)
THEN 0
WHEN d.Last_30D_NGR < 0
THEN 0
ELSE round(d.Last_30D_NGR * f.cash_back_per, 0)
END AS cash_back_calc
,CASE
WHEN f.cash_back_per = 0
THEN 'No Cash Back for Status'
WHEN c.LTGGR - e.LT_TotBonus < 0
THEN 'LT NGR <0'
WHEN d.Last_30D_RM < 0
THEN 'No RM Deposit in 30D'
WHEN e.LT_TotBonus/c.LTRM * 100 >= f.cash_back_BO_per * 100
THEN 'Above LT Bonus Percentage'
WHEN coalesce(g.CashBack_Last30D, 0) > 0
AND round(d.Last_30D_NGR * f.cash_back_per, 0) - coalesce(g.CashBack_Last30D, 0) < 0
THEN 'CashBack in 30D> Cash Back Calculated'
WHEN isnull(d.Last_30D_NGR)
THEN 'No NGR in 30D'
WHEN d.Last_30D_NGR < 0
THEN 'Negative NGR in 30D'
ELSE ''
END AS note
FROM (
(
SELECT *
FROM players_levels
WHERE date_format(Date_Status, '%Y-%m') = (
SELECT date_format(max(players_levels.Date_Status), '%Y-%m')
FROM players_levels
)
GROUP BY Customers
) a LEFT JOIN (
SELECT *
FROM players_levels
WHERE date_format(Date_Status, '%Y-%m') = (
SELECT date_format(max(players_levels.Date_Status) - INTERVAL 1 MONTH, '%Y-%m')
FROM players_levels
)
) b ON a.Customers = b.Customers
LEFT JOIN (
SELECT Customers
,sum(Total_Win_Loss) AS LTGGR
,sum(Real_Money) AS LTRM
,sum(Total_Bets) AS LTBETS
FROM Online_customer_activity_v2
GROUP BY Customers
) c ON a.Customers = c.Customers
LEFT JOIN (
SELECT Customers
,sum(Real_Money) AS Last_30D_RM
,sum(Total_Win_Loss) - sum(_Bonuses) AS Last_30D_NGR
FROM Online_customer_activity_v2
WHERE Online_customer_activity_v2.DATE >= (NOW() - INTERVAL 1 MONTH)
GROUP BY Customers
) d ON a.Customers = d.Customers
LEFT JOIN (
SELECT Account
,sum(CASE
WHEN Method LIKE '%BONUS%'
AND (Method NOT LIKE '%CASHBACKBONUS%')
THEN Amount
ELSE 0
END) LT_Bo_No_CashB
,sum(CASE
WHEN Method LIKE 'CASHBACKBONUS'
THEN Amount
ELSE 0
END) LT_CashBack
,sum(CASE
WHEN Method LIKE '%BONUS%'
AND (Method NOT LIKE '%CASHBACKBONUS%')
THEN Amount
ELSE 0
END) + sum(CASE
WHEN Method LIKE 'CASHBACKBONUS'
THEN Amount
ELSE 0
END) AS LT_TotBonus
FROM `Online_dailyfinance_v2`
GROUP BY Account
) e ON a.Customers = e.Account
LEFT JOIN (
SELECT loyalty_points.status_name
,loyalty_points.cash_back_per
,loyalty_points.cash_back_BO_per
FROM loyalty_points
) f ON a.status_name = f.status_name
LEFT JOIN (
SELECT Account
,sum(CASE
WHEN Method LIKE 'CASHBACKBONUS'
THEN Amount
ELSE 0
END) AS CashBack_Last30D
FROM `Online_dailyfinance_v2`
WHERE Online_dailyfinance_v2.DATE >= (NOW() - INTERVAL 1 MONTH)
GROUP BY Account
) g ON a.Customers = g.Account
)
ORDER BY c.LTRM DESC
這不是一個代碼翻譯服務...如果你不能使用子查詢,那麼你將不得不重寫不使用它們。 –
好的。感謝您的有用評論。也許有人可以給我一個提示... –
有一個服務器端PHP處理程序的解決方法,以啓用對JOIN的支持,請參閱[此答案](http://stackoverflow.com/a/31007028/3549014) –