简体   繁体   中英

Laravel DB query builder - using multiple 'from' with aliases

I have following query:

SELECT 
    cd_4.userfield, 
    COUNT( DISTINCT (Case When cd_4.disposition_Last =  'ANSWERED' Then cd_4.uniqueid ELSE NULL END) ) AS Answered_Count, 
    COUNT( DISTINCT (Case When cd_4.disposition_Last <> 'ANSWERED' Then cd_4.uniqueid ELSE NULL END) ) AS Not_Answered_Count 
FROM (
    SELECT  
        cd_2.Userfield,
        cd_2.Start_Date,
        cd_2.uniqueid,
        cd_2.Row_Id_Max, 
        cd_3.disposition AS disposition_Last 
    FROM (
        SELECT 
            cd_1.userfield,
            cd_1.Start_Date,
            cd_1.uniqueid,
            MAX(cd_1.Row_Id) AS Row_Id_Max 
        FROM 
            cdrnew As cd_1
        WHERE (
            cd_1.Start_Date BETWEEN '2020-12-01' AND '2020-12-10'
        )
        AND cd_1.userfield = 'Inbound' 
        GROUP BY 
            cd_1.userfield,
            cd_1.Start_Date,
            cd_1.uniqueid
    ) AS cd_2 
    INNER JOIN cdrnew As cd_3
    ON cd_3.Row_Id = cd_2.Row_Id_Max
    WHERE (
        cd_3.Start_Date BETWEEN '2020-12-01' AND '2020-12-10'
    )
    AND cd_3.userfield = 'Inbound'
) AS cd_4 ;

I have 4 Aliases used for above for table cdrnew named cd_1, cd_2, cd_3 and cd_4

I want to achieve the same using laravel query builder:

I tried with following laravel query:

   $sqlquery = DB::table('cdrnew')->select('cdrnew.userfield')
        ->addSelect(DB::raw('COUNT(DISTINCT (
      Case When cdrnew.disposition = "ANSWERED" Then cdrnew.uniqueid ELSE NULL END
    )) as Answered_Count'))
        ->addSelect(DB::raw('COUNT(DISTINCT (
      Case When cdrnew.disposition <> "ANSWERED" Then cdrnew.uniqueid ELSE NULL END
    )) as Not_Answered_Count'))
         ->addSelect(DB::raw('cdrnew.Start_Date, cdrnew.uniqueid, MAX(cdrnew.Row_Id) AS Row_Id_Max'))
         ->whereBetween('cdrnew.Start_Date', ["2020-12-12", "2020-12-15"])
        ->groupBy('userfield','Start_Date','uniqueid')
        ->join('cdrnew AS cd_1',"cdrnew.Row_Id", "=", "cd_1.Row_Id") 
       ->limit(10)->get();

But I am only able to create two aliases. One which is default from DB::Table(cdrnew) and another from join(cd_1). How can we create multiple 'from' statements along with different aliases in laravel?

Eg:

  1. from cdrnew as cd_1 (Aliases 1).
  2. from cdrnew as cd_2 (Aliases 2).
  3. from cdrnew as cd_3 (Aliases 3).
  4. ..... and so on

The table and from methods work similarly. They can accept 2 parameters, the table (which can be a Closure or a Builder instance) and an alias. The only difference as far as I can tell is that table must be the first method called after DB::

For example, the following query

SELECT * FROM (
    SELECT * FROM table1 WHERE condition = "something"
) AS alias1

Can be written like this using a Closure .

DB::table(function ($subquery) {
    return $subquery->from('table1')->where('condition', 'something');
}, 'alias')
->get();
// Or using PHP7.4 short Closures
DB::table(fn($subquery) => $subquery->from('table1')->where('condition', 'something'), 'alias')->get();

Or like this using a Builder instance.

$alias = DB::table('table1')->where('condition', 'something');
DB::table($alias, 'alias')->get();

So your query could be translated as the following using the builder

$cd_2 = DB::table('cdrnew, cd_1')
    ->select(
        'cd_1.userfield',
        'cd_1.Start_Date',
        'cd_1.uniqueid',
    )
    ->selectRaw('MAX(cd_1.Row_Id) AS Row_Id_Max')
    ->whereBetween('cd_1.Start_Date', ['2020-12-01', '2020-12-10'])
    ->where('cd_1.userfield', 'Inbound')
    ->groupBy(
        'cd_1.userfield',
        'cd_1.Start_Date',
        'cd_1.uniqueid'
    );

$cd_4 = DB::table($cd_2, 'cd_2')
    ->select(
        'cd_2.Userfield',
        'cd_2.Start_Date',
        'cd_2.uniqueid',
        'cd_2.Row_Id_Max', 
        'cd_3.disposition AS disposition_Last'
    )
    ->join('cdrnew AS cd_3', 'cd_3.Row_Id', '=', 'cd_2.Row_Id_Max')
    ->whereBetween('cd_3.Start_Date', ['2020-12-01', '2020-12-10'])
    ->where('cd3.userfield', 'Inbound');

$query = DB::table($cd_4, 'cd_4')
    ->select('cd_4.userfield')
    ->selectRaw('COUNT(DISTINCT(CASE WHEN cd_4.disposition_Last = "ANSWERED" THEN cd_4.uniqueid ELSE NULL END)) AS Answered_Count')
    ->selectRaw('COUNT(DISTINCT(CASE WHEN cd_4.disposition_Last <> "ANSWERED" THEN cd_4.uniqueid ELSE NULL END)) AS Not_Answered_Count');

// Uncomment next line to see the query produced. It should be the same you posted in your question.
// dd($query->toSql());
$results = $query->get();

Or as this using Closures

$query = DB::table(function ($cd_4) {
    return $cd_4->from(function ($cd_2) {
        return $cd_2->from('cdrnew, cd_1')
        ->select(
            'cd_1.userfield',
            'cd_1.Start_Date',
            'cd_1.uniqueid',
        )
        ->selectRaw('MAX(cd_1.Row_Id) AS Row_Id_Max')
        ->whereBetween('cd_1.Start_Date', ['2020-12-01', '2020-12-10'])
        ->where('cd_1.userfield', 'Inbound')
        ->groupBy(
            'cd_1.userfield',
            'cd_1.Start_Date',
            'cd_1.uniqueid'
        );
    }, 'cd_2')
    ->select(
        'cd_2.Userfield',
        'cd_2.Start_Date',
        'cd_2.uniqueid',
        'cd_2.Row_Id_Max', 
        'cd_3.disposition AS disposition_Last'
    )
    ->join('cdrnew AS cd_3', 'cd_3.Row_Id', 'cd_2.Row_Id_Max')
    ->whereBetween('cd_3.Start_Date', ['2020-12-01', '2020-12-10'])
    ->where('cd3.userfield', 'Inbound');
}, 'cd_4')
->select('cd_4.userfield')
->selectRaw('COUNT(DISTINCT(CASE WHEN cd_4.disposition_Last = "ANSWERED" THEN cd_4.uniqueid ELSE NULL END)) AS Answered_Count')
->selectRaw('COUNT(DISTINCT(CASE WHEN cd_4.disposition_Last <> "ANSWERED" THEN cd_4.uniqueid ELSE NULL END)) AS Not_Answered_Count');

// Uncomment next line to see the query produced. It should be the same you posted in your question.
// dd($query->toSql());
$results = $query->get();

As far as I can tell, the queries generated are the same.

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