2016-04-14 67 views
1

我的應用程序中有以下sql查詢。laravel MySQL查詢優化

$defaultGoal = DB::table("goals") 
-> where("activitiesID", "=", $this->activityID) 
-> where("usersID", "=", $userID) 
-> pluck("goal"); 

$defaultGoalPoints = DB::table("goals") 
-> where("activitiesID", "=", $this->activityID) 
-> where("usersID", "=", $userID) 
-> pluck("goalpoints"); 

$defaultPoints = DB::table("goals") 
-> where("activitiesID", "=", $this->activityID) 
-> where("usersID", "=", $userID) 
-> pluck("points"); 

每個查詢返回一個數組。有沒有辦法將所有這些查詢合併爲一個,這將返回相同的數組,$defaultPoints,$defaultGoalPoints$defaultGoal

+0

如果目標是減少你寫的查詢,大約只需使用什麼樣的數量 - > get()檢索整個條目,然後使用php將你想要的數據拖入數組中? (雖然Laravel可能有一個更短的方法來做到這一點,如果他們知道這一點,我會讓其他人說出來。) – Atlas

回答

1

您可以使用select只需要得到什麼:

$defaults = DB::table("goals") 
->select('goal', 'goalpoints', 'points') 
->where("activitiesID", "=", $this->activityID) 
->where("usersID", "=", $userID) 
->get(); 

希望它可以幫助...