2016-08-28 70 views
-1

我在我的應用程序中使用Laravel 5.1。我的MySQL查詢是將一個Mysql查詢轉換成Laravel 5雄辯?

"Select * from " 
     . "(Select u.* from " 
     . "(" 
     . "SELECT u2.id as user_id, u2.fname as fname, u2.lname as lname, u2.uname as uname, u2.email as email, u2.address as address, u2.city_id as city_id, u2.website as website FROM users u1, users u2 where u1.city_id = u2.city_id && u1.id = '$this->current_user_id') 
      u left join follows f on u.user_id = f.following_id where f.following_id is null 
      UNION 
      Select uu.id as user_id, uu.fname as fname, uu.lname as lname, uu.uname as uname, uu.email as email, uu.address as address, uu.city_id as city_id, uu.website as website from users uu where uu.id in (Select u.user_id from (SELECT user_id FROM reviews group by user_id order by count(user_id) desc5 
      ) u left join follows f on u.user_id=f.following_id where f.following_id is null 
      ) 
      ) 
      T LIMIT 5" 

如何將它轉換成Laravel雄辯?

+2

你試過轉換它嗎? –

+0

desc5 ??什麼是 – Strawberry

+0

Eloquent是一個ORM。因此,您需要[創建適當的模型](https://laravel.com/docs/5.1/eloquent#defining-models)和[關係](https://laravel.com/docs/5.1/eloquent-relationships #定義-關係)。 – Johnny

回答

0

是該查詢功能上等同於這一個:

SELECT u2.id user_id 
    , u2.fname 
    , u2.lname 
    , u2.uname 
    , u2.email 
    , u2.address 
    , u2.city_id 
    , u2.website 
    FROM users u1 
    JOIN users u2 
    ON u2.city_id = u1.city_id 
    LEFT 
    JOIN follows f 
    ON f.following_id = u.user_id 
WHERE f.following_id is null 
UNION 
SELECT uu.id user_id 
    , uu.fname 
    , uu.lname 
    , uu.uname 
    , uu.email 
    , uu.address 
    , uu.city_id 
    , uu.website 
    FROM users uu 
    JOIN 
    (SELECT r.user_id 
     FROM reviews r 
     LEFT 
     JOIN follows f 
      ON f.following_id = r.user_id 
     WHERE f.following_id IS NULL 
     GROUP 
      BY user_id 
     ORDER 
      BY COUNT(user_id) DESC 
     LIMIT 5 
    ) x 
    ON x.user_id = uu.id; 

+0

但我想轉換laravel雄辯..? –

+0

似乎更容易轉換爲我 – Strawberry

+0

此外,是不是有某種DB :: raw()功能 – Strawberry