Birleştirme Deyimleri


    × Bunları biliyor muydunuz?
"Settings/CDNLinks.php yapılandırma dosyası altında yer alan 'script' ve 'style' dizilerine tanımlayacağınız linkleri Import::script() ve Import::style() kütüphaneleri ile birlikte kullanabilirsiniz."


Bu bölümde SQL işlemlerinde en çok kullanılan yapılar olan tablo birleştirmelerin ZN ile nasıl yapıldığı gösterilmiştir. ZN Framework ile tablo birleştirme işlemlerini son derece basit bir şekilde gerçekleştirebilmeniz mümkün.

 

 

# Yöntemler


DB::innerJoin(string $table, string $otherColumn, string $operator = '=') : this
DB::outerJoin(string $table, string $otherColumn, string $operator = '=') : this
DB::leftJoin(string $table, string $otherColumn, string $operator = '=') : this
DB::rightJoin(string $table, string $otherColumn, string $operator = '=') : this
DB::union(string $table) : this
DB::unionAll(string $table) : this

 

 

# InnerJoin


Tablolar arasında SQL INNER JOIN deyimi ile birleştirme yapmak için kullanılır. 1. Parametre birleştirilecek diğer tablo adını ve kolon bilgisini içermelidir.

string $joinTableColumn
Birleştirilecek tablo ve kolon bilgisi.
string $tableColumn Tek kullanım için get() yöntemine yazılan tablo ve kolon bilgisi olmalıdır. Çoklu kullanımda farklı tablo ve kolon bilgisi olabilir.
return this

Kullanımlar

Birleştirme işlemine konu olmuş tablolar ile hangi işlemlerin yapıldığı aşağıda örneklendirilmiştir.

Select

DB::innerJoin('comments.user_id', 'profiles.user_id')
  ->innerJoin('profiles.user_id', 'users.id')
  ->users();
SELECT *
FROM users
INNER JOIN comments ON comments.user_id = profiles.user_id
INNER JOIN profiles ON profiles.user_id = users.id

Update

Inner Join ile update işlemi

DB::where('table1.id', 20)->innerJoin('table2.user_id', 'table1.id')->update('table1',
[
    'table1.name'  => 'Example Name',
    'table2.phone' => '0000000000'
]);
UPDATE table1
INNER JOIN table2 ON table2.user_id = table1.id
SET table1.name='Example Name',table2.phone='0000000000'
WHERE table1.id = 20

Delete

Inner Join ile delete işlemi

DB::where('table1.id', 20)->innerJoin('table2.user_id', 'table1.id')->delete('table1');
DELETE table1, table2 FROM table1
INNER JOIN table2 ON table2.user_id = table1.id
WHERE table1.id = 20 

 

 

# OuterJoin


Tablolar arasında SQL FULL OUTER JOIN deyimi ile birleştirme yapmak için kullanılır. 1. Parametre birleştirilecek diğer tablo adını ve kolon bilgisini içermelidir.

string $joinTableColumn
Birleştirilecek tablo ve kolon bilgisi.
string $tableColumn Tek kullanım için get() yöntemine yazılan tablo ve kolon bilgisi olmalıdır. Çoklu kullanımda farklı tablo ve kolon bilgisi olabilir.
return this

Kullanımlar

DB::outerJoin('comments.user_id', 'profiles.user_id')
  ->outerJoin('profiles.user_id', 'users.id')
  ->users();
SQL Code:
SELECT *
FROM users
FULL OUTER JOIN comments ON comments.user_id = profiles.user_id
FULL OUTER JOIN profiles ON profiles.user_id = users.id

 

 

# LeftJoin


Tablolar arasında SQL LEFT JOIN deyimi ile birleştirme yapmak için kullanılır. 1. Parametre birleştirilecek diğer tablo adını ve kolon bilgisini içermelidir.

string $joinTableColumn
Birleştirilecek tablo ve kolon bilgisi.
string $tableColumn Tek kullanım için get() yöntemine yazılan tablo ve kolon bilgisi olmalıdır. Çoklu kullanımda farklı tablo ve kolon bilgisi olabilir.
return this

Kullanımlar

DB::leftJoin('comments.user_id', 'profiles.user_id')
  ->leftJoin('profiles.user_id', 'users.id')
  ->users();
SQL Code:
SELECT *
FROM users
LEFT JOIN comments ON comments.user_id = profiles.user_id
LEFT JOIN profiles ON profiles.user_id = users.id

 

 

# RightJoin


Tablolar arasında SQL RIGHT JOIN deyimi ile birleştirme yapmak için kullanılır. 1. Parametre birleştirilecek diğer tablo adını ve kolon bilgisini içermelidir.

string $joinTableColumn
Birleştirilecek tablo ve kolon bilgisi.
string $tableColumn Tek kullanım için get() yöntemine yazılan tablo ve kolon bilgisi olmalıdır. Çoklu kullanımda farklı tablo ve kolon bilgisi olabilir.
return this

Kullanımlar

DB::rightJoin('comments.user_id', 'profiles.user_id')
  ->rightJoin('profiles.user_id', 'users.id')
  ->users();
SQL Code:
SELECT *
FROM users
RIGHT JOIN comments ON comments.user_id = profiles.user_id
RIGHT JOIN profiles ON profiles.user_id = users.id

 

 

# Union


Tablolar arasında SQL UNION deyimi ile birleştirme yapmak için kullanılır.

string $joinTableColumn
Birleştirilecek tablo ve kolon bilgisi.
return this

Kullanımlar

$users = DB::select('id', 'name')->union('example')->select('id', 'phone')->users();

output($users->stringQuery());
output($users->result());
SELECT id,name FROM example UNION SELECT id,phone FROM users
0 => object
(
        id => string '2' ( length = 3 )
        name => string 'Example' ( length = 9 )
)
1 => object
(
        id => string '3' ( length = 3 )
        name => string 'Test' ( length = 5 )
)
2 => object
(
        id => string '1' ( length = 3 )
        name => string '55332587488' ( length = 7 )
)
3 => object
(
        id => string '2' ( length = 3 )
        name => string '88996544878' ( length = 4 )

 

 

# UnionAll


Tablolar arasında SQL UNION ALL deyimi ile birleştirme yapmak için kullanılır.

string $joinTableColumn
Birleştirilecek tablo ve kolon bilgisi.
return this

Kullanımlar

$users = DB::select('id', 'name')->unionAll('example')->select('id', 'phone')->users();

output($users->stringQuery());
output($users->result());
SELECT id,name FROM example UNION ALL SELECT id,phone FROM users
0 => object
(
        id => string '2' ( length = 3 )
        name => string 'Example' ( length = 9 )
)
1 => object
(
        id => string '3' ( length = 3 )
        name => string 'Test' ( length = 5 )
)
2 => object
(
        id => string '1' ( length = 3 )
        name => string '55332587488' ( length = 7 )
)
3 => object
(
        id => string '2' ( length = 3 )
        name => string '88996544878' ( length = 4 )