DB::DBAL

The World's Most Powerful Light-Weight DBAL

Joins

There are four join functions: join, innerjoin, leftjoin, and rightjoin.

join( string $table_name, string $table_short_name, string $on, [string $type = 'inner'] )

You can create a join by using a shared column or by using the equals (“=”) syntax as shown below:

1
2
3
4
5
6
7
// Join on Shared Column
DB::MySQL()->table('myTable', 'short')->select()
->join('myOtherTable', 'short2', 'sharedColumn');
// Join using Equals Syntax
DB::MySQL()->table('myTable', 'short')->select()
->join('myOtherTable', 'short2', 'short1.column = short2.column');

You can also determine the type of join within the join() method by declaring it’s type as a fourth argument (inner, left, right). However, for convenience, you are also able to use the following methods below.

innerjoin( string $table_name, string $table_short_name, string $on)

leftjoin( string $table_name, string $table_short_name, string $on)

rightjoin( string $table_name, string $table_short_name, string $on)

Join using a shared column:

1
2
3
4
5
6
7
8
DB::MySQL()->table('myTable', 'short')->select()
->innerjoin('myOtherTable', 'short2', 'sharedColumn');
DB::MySQL()->table('myTable', 'short')->select()
->leftjoin('myOtherTable', 'short2', 'sharedColumn');
DB::MySQL()->table('myTable', 'short')->select()
->rightjoin('myOtherTable', 'short2', 'sharedColumn');

Or Join using Equals Syntax:

1
2
3
4
5
6
7
8
DB::MySQL()->table('myTable', 'short')->select()
->innerjoin('myOtherTable', 'short2', 'short1.column = short2.column');
DB::MySQL()->table('myTable', 'short')->select()
->leftjoin('myOtherTable', 'short2', 'short1.column = short2.column');
DB::MySQL()->table('myTable', 'short')->select()
->rightjoin('myOtherTable', 'short2', 'short1.column = short2.column');

Note: you may also the table short name in the select(), where(), orderBy(), and groupBy() methods.