简体   繁体   中英

SQL Multiple result sets

在此处输入图片说明 I am joining 4 tables to retrive but, i want to retrive result set as 2 arrays, i tried and its retriving as one result set.

Following is query

 select * from `services` inner join `location_services` on `location_services`.`serviceID` = `services`.`serviceID` inner join `locations` on `locations`.`locationID` = `location_services`.`locationID` inner join `clinics` on `clinics`.`clinicID` = `locations`.`clinicID` where `locations`.`clinicID` = 7

As shown in picture, i want to retrive

location id with 38 as one result set and `39` as another.

Follwing is my query in controller

public function showClinic($id)
    {
        $clinic = Clinic::find($id);
        $locations = Location::where('clinicID', $id)->get();
        $locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid')
            ->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
            ->join('locations', 'locations.locationID', '=', 'location_services.locationID')
            ->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
            ->where('clinics.clinicID','=',$id)
            ->get();

        // $newlocations = Service::select('services.serviceName as servicename','locations.locationID as locid')
        //     ->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
        //     ->join('locations', 'locations.locationID', '=', 'location_services.locationID')
        //     ->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
        //     ->where('clinics.clinicID','=',$id)
        //     ->get();
        return view('clinic.show')->with(['locations' =>  $locations  ,'clinic'=>$clinic , 'services'=> $locationservices]);

    }

Here there are multiple clin

Add ->groupBy('location_services.locationID') to your query, so you can group the same locationID as one record:

public function showClinic($id)
    {
        $clinic = Clinic::find($id);
        $locations = Location::where('clinicID', $id)->get();
        $locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid')
            ->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
            ->join('locations', 'locations.locationID', '=', 'location_services.locationID')
            ->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
            ->where('clinics.clinicID','=',$id)
            ->groupBy('location_services.locationID')
            ->get();


        return view('clinic.show')->with(['locations' =>  $locations  ,'clinic'=>$clinic , 'services'=> $locationservices]);

    }

And if your mysql version is v5.7+ , you will have ONLY_FULL_GROUP_BY problem,

add modes to mysql array in your config/database.php:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            ...
            'strict' => true,
            'modes'  => [
                //'ONLY_FULL_GROUP_BY',
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_ENGINE_SUBSTITUTION',
            ]
        ],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM