peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Lệnh Left Join trong SQL Server - Freetuts
GETTING STARTED
DATA DEFINITION
DATA QUERIES
CONDITION & OPERATORS
EXPLORED
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Lệnh Left Join trong SQL Server

Trong bài này chúng ta sẽ tìm hiểu Left Join trong SQL Server. Nếu Inner Join là tích hai bảng và chỉ lấy các dòng trùng khóa ngoại với nhau thì Left Join sẽ lấy thêm cả các dòng mà bảng bên trái có nhưng bên phải không có.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Cú pháp Left Join trong SQL Server

Left Join sẽ hợp hai bảng lại và lấy những cặp record thỏa mãn ở điều kiện ON + Những dòng dữ liệu ở bảng bên trái có nhưng bảng bên phải không có.

Bạn có đọc phần lý tuyết về Left Join trong SQL để hiểu rõ hơn. Trong bài này mình chỉ nói đến cú pháp và cách dùng mà thôi.

Cú pháp:

Bài viết này được đăng tại [free tuts .net]

SELECT
    select_list
FROM
    T1
LEFT JOIN T2 ON
    join_predicate;

Trong đó select_list là những field mà bạn muốn câu lệnh này tra về. Bạn có thể chọn các field bất kì, miễn là nó thuộc một trong hai bảng T1T2.

join_predicate chính là điều kiện kiểm tra ràng buộc giữa hai bảng. Nếu dòng record nào thỏa điều kiện này thì ta gọi là thỏa điều kiện ON.

Left Join sẽ trả về những dòng thỏa điện kiện ở ON với nhau và những dòng mà bảng T1 có dữ liệu nhưng bảng T2 không có.

SQL Server LEFT JOIN png

2. Ví dụ về Left Join trong SQL Server

Hãy thực hành ví dụ để bạn hiểu rõ hơn lệnh Left Join trong SQL Server.

Giả sử ta có ba table gồm: products, order_itemsorders có mối liên hệ như lược đồ diagram sau:

orders order items products png

Mỗi đơn hàng sẽ có nhiều sản phẩm, vì vậy ta sẽ có bảng trung gian nằm giữa để chia mối quan hệ (n - n) này thành hai mối quan hệ mới:

  • Gữa productsorder_items là (1 - n)
  • Giữa ordersorder_items là (1 - n)

Bây giờ cần lấy danh sách tên sản phẩm và mã số order_id của các đơn hàng, nhưng với điều kiện là lấy luôn những sản phẩm không có đơn nào, vì vậy ta sẽ đặt bảng products nằm bên trái và order_items nằm bên phải.

SELECT
    product_name,
    order_id
FROM
    production.products p
LEFT JOIN sales.order_items o ON o.product_id = p.product_id
ORDER BY
    order_id;

SQL Server Left Join example png

Như bạn thấy, có một số sản phẩm không có đơn hàng nào và cung có một số sản phẩm có nhiều đơn hàng. Như trong hình thì ta thấy có nhiều sản phẩm nằm trong đơn hàng có order_id = 1.

Đấy là join 2 bảng, bây giờ ta sẽ làm một ví dụ join 3 bảng với nhau.

SELECT
    p.product_name,
    o.order_id,
    i.item_id,
    o.order_date
FROM
    production.products p
	LEFT JOIN sales.order_items i
		ON i.product_id = p.product_id
	LEFT JOIN sales.orders o
		ON o.order_id = i.order_id
ORDER BY
    order_id;

Với trường hợp này thì SQL Server sẽ thực hiện 2 lần Join như sau:

  • Lần thứ nhất join bảng products với order_items => tạo ra danh sách các record và nói là một bảng tạm. Lúc này bảng products là bảng bên trái.
  • Lần thứ hai join bảng tạm đó với bảng orders, lúc này bảng tạm chính là bảng bên trái.

Và đây là kết quả:

SQL Server LEFT JOIN join three tables png

3. Thêm lệnh Where trong Left Join

Nếu bạn muốn lọc thêm điều kiện thì hãy đặt nó trong lệnh WHERE nhé.

Giả sử mình cần lấy thông tin tên các sản phẩm mã đơn hàng của đơn hàng có order_id = 100.

SELECT
    product_name,
    order_id
FROM
    production.products p
LEFT JOIN sales.order_items o 
   ON o.product_id = p.product_id
WHERE order_id = 100
ORDER BY
    order_id;

Như bạn thấy mình đã đặt thêm điều kiện WHERE order_id = 100 để lọc dữ liệu theo yêu cầu bài toán.

SQL Server Left Join and WHERE clause png

Hãy thử di chuyển order_id = 100 vào ON xem thế nào nhé.

SELECT
    p.product_id,
    product_name,
    order_id
FROM
    production.products p
    LEFT JOIN sales.order_items o 
         ON o.product_id = p.product_id AND 
            o.order_id = 100
ORDER BY
    order_id DESC;

SQL Server LEFT JOIN move condition to ON clause png

Kết quả hoàn toàn khác, bởi vì ta đang sử dụng Left Join nên nó trả về luôn cả những record mà bảng bên trái có dữ liệu mà bảng bên phải không có.

Trên là tất cả những kiến thức cơ bản về lệnh Left Join trong SQL Server. Nếu còn vướng mắc thì hãy đặt câu hỏi ở phần bình luận nhé.

Cùng chuyên mục:

Hàm CHAR trong SQL Server

Hàm CHAR trong SQL Server

Cách sử dụng hàm CHAR trong SQL Server

Hàm CHARINDEX trong SQL Server

Hàm CHARINDEX trong SQL Server

Cách sử dụng hàm CHARINDEX trong SQL Server

Hàm CONCAT trong SQL Server

Hàm CONCAT trong SQL Server

Cách sử dụng hàm CONCAT trong SQL Server

Hàm + trong SQL Server

Hàm + trong SQL Server

Cách sử dụng hàm + trong SQL Server

Hàm DATALENGTH trong SQL Server

Hàm DATALENGTH trong SQL Server

Cách sử dụng hàm DATALENGTH trong SQL Server

Hàm LEFT trong SQL Server

Hàm LEFT trong SQL Server

Cách sử dụng hàm LEFT trong SQL Server

Hàm LEN trong SQL Server

Hàm LEN trong SQL Server

Cách sử dụng hàm LEN trong SQL Server

Hàm LOWER trong SQL Server

Hàm LOWER trong SQL Server

Cách sử dụng hàm LOWER trong SQL Server

Hàm LTRIM trong SQL Server

Hàm LTRIM trong SQL Server

Cách sử dụng hàm LTRIM trong SQL Server

Hàm NCHAR trong SQL Server

Hàm NCHAR trong SQL Server

Cách sử dụng hàm NCHAR trong SQL Server

Hàm PATINDEX trong SQL Server

Hàm PATINDEX trong SQL Server

Cách sử dụng hàm PATINDEX trong SQL Server

Hàm REPLACE trong SQL Server

Hàm REPLACE trong SQL Server

Cách sử dụng hàm REPLACE trong SQL Server

Hàm RIGHT trong SQL Server

Hàm RIGHT trong SQL Server

Cách sử dụng hàm RIGHT trong SQL Server

Hàm RTRIM trong SQL Server

Hàm RTRIM trong SQL Server

Cách sử dụng hàm RTRIM trong SQL Server

Hàm SPACE trong SQL Server

Hàm SPACE trong SQL Server

Cách sử dụng hàm SPACE trong SQL Server

Hàm STR trong SQL Server

Hàm STR trong SQL Server

Cách sử dụng hàm STR trong SQL Server

Hàm UPPER trong SQL Server

Hàm UPPER trong SQL Server

Cách sử dụng hàm UPPER trong SQL Server

Hàm SUBSTRING trong SQL Server

Hàm SUBSTRING trong SQL Server

Cách sử dụng hàm SUBSTRING trong SQL Server

Hàm STUFF trong SQL Server

Hàm STUFF trong SQL Server

Cách sử dụng hàm STUFF trong SQL Server

Hàm ABS trong SQL Server

Hàm ABS trong SQL Server

Cách sử dụng hàm ABS trong SQL Server

Top