2015-09-23 110 views
1

我在我的應用程序中使用Laravel 5.1。將Mysql查詢轉換成Laravel雄辯

我的MySQL查詢是

SELECT * FROM (SELECT uu.*,t.left_id AS meb_id,(CAST(t.pair AS UNSIGNED) - IFNULL(p.pair,0)) AS pair FROM (SELECT left_id, 
(CASE 
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000') >= (SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2) 
THEN (SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000') 
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000') < (SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2) 
THEN ((SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000') /2) 
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000') >= (SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2) 
THEN (SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000') 
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000') < (SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2) 
THEN ((SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000')/2) END 
) AS pair 
FROM user_count AS u WHERE left_id <> 0 GROUP BY left_id) AS t 
LEFT JOIN users AS uu ON uu.`id` = t.left_id 
LEFT JOIN `total_payment` p ON t.left_id = p.`meb_id` 
WHERE t.pair <> 0) AS f WHERE f.pair > 0 AND f.active = 1 

我怎樣才能把它轉換在Laravel雄辯?我不知道如何在laravel雄辯中使用when case

我的數據庫架構是這樣的

user_countenter image description here

userenter image description here

total_payment

enter image description here

任何人都可以幫助我嗎?

+0

不知道我明白你爲什麼想要用數據庫正面使用直接SQL查詢來進行這項工作。 – Pitchinnate

+0

@Pitchinnate 這個查詢需要太多的時間來執行,所以我想如果我用雄辯運行它會更好。 –

+1

我不認爲你明白了那時的雄辯。 – Pitchinnate

回答

0

Eloquent是一個ORM(對象關係映射),它意味着您可以將數據庫映射到PHP對象(Eloquent使用Active Record模式)。

它不是DBAL,但仍然可以通過數據庫外觀執行SQL查詢。

您提供的示例中,user表可以是一個Eloquent對象。

class User extends Model{ 
    /** 
    * The table associated with the model. 
    * 
    * @var string 
    */ 
    protected $table = 'user'; 
} 

既然現在你可以獲取所有用戶像行:

$userCollection = (new User)->all(); //Fetch all rows 

或者您也可以通過PK就像找到一個:

$user = (new User)->find(123); //123 is the PK of the User 
+0

我已經使用數據庫外觀運行此查詢。 但我想將此查詢轉換爲Laravel Eloquent。 那麼我該如何做到這一點? 我爲每個表格創建了模型。 –

1

只是圍繞您的原始查詢與DB ::聲明(「」),如下所示:

DB::statement("INSERT INTO `mydatabase`.`mytable` (`id`, `created_at`, `updated_at`, `deleted_at`) VALUES ('7', '2015-09-26 13:38:48', '2015-09-26 13:43:41', NULL)"); 

它會做的很好,並與種子一起工作。

+0

我已經在使用'DB :: statement(「」)'運行這個查詢了。 我想用'Eloquent'運行它 –