laravel db::raw(选择)

c9x0cxw0  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(236)

sql语句

Trip::select(
        'trips.id',

        DB::raw('
            (      
                    SELECT 
                        DATE_FORMAT(
                            FROM_UNIXTIME(
                                UNIX_TIMESTAMP(trips.date_trip) + start_time
                            )
                            , "%H:%i"
                        ) 
                    FROM 
                        trip_regions 
                    WHERE 
                        region_id="'.$from.'" and trip_id=trips.id
            ) 
                    as date_star
        '),

        DB::raw('
                (
                    SELECT 
                        DATE_FORMAT(
                            FROM_UNIXTIME(
                                UNIX_TIMESTAMP(trips.date_trip) + start_time
                            )
                            , "%H:%i (%d %M)"
                        ) 
                    FROM 
                        trip_regions 
                    WHERE 
                        region_id="'.$where.'" and trip_id=trips.id
                ) 
                    as date_end
        '),

        'trips.regions','trips.type_trips','trips.mail','trips.price','trips.price_mail',
        'trips.passenger','trips.min_weigh_ton','trips.max_weigh_ton','trips.contract','trips.multiple_booking',
        'trips.smoking','trips.animals','trips.people_back','trips.small_cargo','trips.with_place','trips.delivery_door',
        'trips.date_trip','trips.date_back','trips.user_id','trips.car_id',
        DB::raw('
            (
                SELECT
                        DATE_FORMAT(
                            FROM_UNIXTIME(
                                UNIX_TIMESTAMP(trips.date_trip) + start_time
                            )
                            , "%Y-%m-%d %H:%i:%s"
                        ) as time
                FROM
                    trip_regions
                WHERE 
                    region_id="'.$from.'" and trip_id=trips.id
            ) as date
        ')

    )

        ->where(function($query) use ($req) {
            if($req->type == 1){
                $query->where('trips.type_trips','passenger');
            }elseif($req->type == 2){
                $query->where('trips.mail',1);
            }elseif($req->type == 3){
                $query->where('trips.type_trips','cargo');
            }
        })
        ->where('trips.regions','like','%|f|'.$from."|%")
        ->where('trips.regions','like','%|w|'.$where."|%")
       //->whereBetween('date',[$date, $date_today])
        ->havingRaw('date between "'. $date.'" and "'. $date_today.'"')

        ->with([
            'user_id' => function($query){
                $query->select('id','firstname','lastname','avatar');
            },
            'car_id' => function($q){
                $q->select('id','car_name','color','year')->with(
                    ['car_name' => function($qq){
                        $qq->select('id','name','car_models_id')->with(
                            ['car_models_id' => function($qqq){
                                $qqq->select('id','name');
                            }]
                        );
                    }]
                );
            },
            'trips_regions' => function($query) use ($from, $where){
                $query->select('id','region_id','trip_id','price','km','type')
                    ->whereIn('region_id',[$from,$where])
                    ->orderBy(DB::raw('FIELD(region_id, '.$from.','.$where.') '))
                    //->where('region_id',$where)
                    ->with([
                        'region_id' => function($qqq) {
                            $qqq->select('id', 'name_ru');
                        }
                    ]);
            },
            'orders' => function($order){
                $order->select('id','trip_id','count_order');
            }
        ])
        ->orderBy('date','asc')
        ->paginate(10);

这是完整的代码。我已经采取了这个从旅行地区和增加旅行。日期旅行和获得旅行日期应该检查通过之间,但正如你所看到的,这是不可能有头痛(
错误

"Column not found: 1054 Unknown column 'date' in 
'having clause' (SQL: select count(*) as aggregate from `trips` where 
(`trips`.`type_trips` = passenger) and .... or .... and ... having date between "2018-05-14 00:00:00" and "2018-05-14 
23:59:59" and date between "2018-05-14 00:00:00" and "2018-05-14 23:59:59")"

大家好,我尝试了很多次,什么都没有发生请帮助这个代码已经2天我找不到解决方案。上一页谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题