mercredi 3 février 2016

How to make complexe query with laravel Query Builder

Let consider the senario where i have this table parameters with some sample data. enter image description here

I need to query these data into three categories as describing bellow.

query1 = get all records that has: param1=param2=param3= 100% as from this case the oupout is the first record. Not problem with this query.

query2 = get all records that has: at least param1 < 80 OR param2 < 80 OR param3 < 80 OR all as from this case the outpout is the second and the third record.

query3 = get all records that has: at least param1 >= 80 OR param2 >= 80 OR param3 >= 80 BUT NOT ALL EQUAL to 100% as from this case the outpout is the fourth and fifth records.

I am really stock at query2 and query3. bellow is my query using laravel query builder.

            $query = DB::table('parameters');                
            if ($query === 1) {
                $query->where('param1', '=', 100)
                      ->where('param2', '=', 100)
                      ->where('param3', '=', 100);
            }elseif ($query === 2) {
                $query->whereBetween('param1', [80, 100])
                      ->whereBetween('param2', [80, 100])
                      ->whereBetween('param3', [80, 100]);
            }else{
                $query->where('param1', '<', 80)
                      ->where('param2', '<', 80)
                      ->where('param3', '<', 80);
            }
            $result = $query->get();

Hope my question is clair enougth. Thanks in advance for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire