对Laravel Eloquent查询的联合查询

时间:2018-05-10 06:53:50

标签: php mysql laravel eloquent laravel-5.2

需要帮助才能将此查询转换为laravel Eloquent Query

select `pin`.*,`pin_reviews`.`review`,( 3959 * acos( cos( radians('27.788870') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('72.978920') ) + sin( radians('27.788870') ) * sin( radians( lat ) ) ) ) AS distance,COUNT(`pin_reviews`.`pin_id`) as `totalUserRating`,SUM(`pin_reviews`.`rating`) / (COUNT(`pin_reviews`.rating * 5)) as `rating`,`ads`.`product_name`,`ads`.`id` as 'adId',`ads`.`notes`,`ads`.`add_status`,`ads`.`description` as 'adsDescription',`c`.`name` as 'catName',`c`.`id` as 'catId',`sc`.`id` as 'subcatId', `sc`.`name` as 'subName',`ssc`.`id` as 'SubsubId',`ssc`.`name` as 'subsubName' from ( 

  ( SELECT
        *
    FROM
        pin WHERE `subscription_plan` = 'Premium' LIMIT 2)
    UNION 
       ( SELECT
            *
        FROM
            pin WHERE `subscription_plan` = 'Platinum' LIMIT 1)
    UNION
        (SELECT
            *
            FROM pin WHERE `subscription_plan` = 'Free' LIMIT 10)
)

 AS pin
left join `pin_reviews` on `pin_reviews`.`pin_id` = `pin`.`id` left 
join `ads` on `ads`.`pin_id` = `pin`.`id` left join `category` as `c` 
on `c`.`id` = `ads`.`category_id` left join `category` as `sc` on 
`sc`.`id` = `ads`.`sub_category_id` left join `category` as `ssc` on 
`ssc`.`id` = `ads`.`sub_sub_category` where `pin`.`status` = 1 group 
by 
`pin`.`id` having `distance` < 380 order by `distance` asc limit 15

2 个答案:

答案 0 :(得分:2)

Laravel eloquent支持=SUMPRODUCT(0+ISNUMBER(SEARCH(" "&TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)*(ROW(INDEX(A:A,1):INDEX(A:A,1+LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1)+1,LEN(A1)))&" "," "&B1&" ")))>0,您可以在其中进行另一次查询,但在这种情况下,将所有查询放入union()

会更简单

https://laravel.com/docs/5.6/queries#unions

https://laravel.com/docs/5.6/queries#raw-expressions

答案 1 :(得分:1)

   $pinData =  Pin::select(
        DB::raw("`pin`.*,`pin_reviews`.`review`,( 3959 * acos( cos( 
    radians('$lat') ) * cos( radians( lat ) ) * cos( radians( lng ) - 
    radians('$lng') ) + sin( radians('$lat') ) * sin( radians( lat ) ) 
    ) ) 
  AS distance,COUNT(`pin_reviews`.`pin_id`) as
`totalUserRating`,SUM(`pin_reviews`.`rating`) / 
(COUNT(`pin_reviews`.rating * 5)) as 
`rating`,`ads`.`product_name`,`ads`.`id` as 
'adId',`ads`.`notes`,`ads`.`add_status`,`ads`.`description` as 
'adsDescription',`c`.`name` as 'catName',`c`.`id` as 'catId',`sc`.`id` 
as 'subcatId', `sc`.`name` as 'subName',`ssc`.`id` as 
'SubsubId',`ssc`.`name` as 'subsubName' " ));


$pinData->LeftJoin('pin_reviews','pin_reviews.pin_id','=', 
'pin.id');
    $pinData->LeftJoin('ads','ads.pin_id','=', 'pin.id');
    $pinData->LeftJoin('category as c','c.id','=', 'ads.category_id');
    $pinData->LeftJoin('category as sc','sc.id','=', 
'ads.sub_category_id');
    $pinData->LeftJoin('category as ssc','ssc.id','=', 
'ads.sub_sub_category');
    //$pinData->where('pin.status',1);
    $pinData->groupBy('pin.id');
    $pinData->Having('distance','<',1870);
$qcon    = '';
    // if search keyword 
    if($search!='')
    {
        $qcon.= " AND (pin.pin_name LIKE '%".$search."%'";
        $qcon.= " OR pin.name_on_map LIKE '%".$search."%'";
        $qcon.= " OR pin.description LIKE '%".$search."%'";
     }
    if($adStatus!='')
    {
            $qcon.= " AND ads.add_status='". $adStatus."' ";
    }


    $pinData->whereRaw('( 1 '.$qcon.')'); 
    $pinData->OrderBy('distance');
    $pinData->limit($search_settings->pins_appear_limit);
    $pins   = $pinData->get();