يعتمد مفهوم ال Left join على جدول ايسر وجدول أيمن كما هو موضح في الصورة
الجدول الأيسر ويشار اليه ب Table1 والجدول الأيمن ويشار اليه ب Table2
مهمة ال Left join هو جلب كافة السجلات من الجدول الأيسر وجلب فقط السجلات المشتركة من الجدول الأيمن مع وضع قيمة Null اما السجل الغير مرتبط
كيف ؟
في #المثال التالي سوف نوضح ذلك خطوة بخطوة
لنفترض ان لدينا جدول الزبائن Customer يحتوي على البيانات التالية رقم الزبون – اسم الزبون – رقم الهاتف
CUST_ID CUST_NAME CUST_PHONE
N1 OMER 1269
N2 GAMAL 1259
N3 MOHAMMED 1249
N4 KHALED 1239
ولدينا أيضآ جدول الطلبيات ORDER يحتوي على رقم الطلبية ورقم الزبون وتاريخ الطلبية
ORDER_ID CUST_ID ORDER_DATE
O1 N1 09/09/2018
O2 N2 10/09/2018
O3 N3 11/09/2018
لاحظ ان جدول ال ORDER مرتبط بجدول ال CUSTOMER بواسطة CUST_ID الذي هو رقم الزبون
ولاحظ أيضآ ان الزبون KHALED الذي لديه ال ID = N4 لاتوجد لديه طلبية
لنفترض اننا نريد طباعة تقرير يحتوي فقط على الزبائن الذين لديهم طلبيات طبعآ الزبون الذي لديه طلبيه ستجد ال ID الخاص به مسجل في جدول الطلبيات ORDER في حالة اذا اردت جلب السجلات المشتركة فقط اي الزبائن الذيز لديهم طلبية سوف نستخدم ال inner join التي تحدثنا عنها في الدرس السابق التي فقط ستقوم بطباعة السجلات المشتركة في هذه الحالة لن يتم طباعة الزبون KHALED لانه ليس لديه طلبية لأن ال ID الخاص به غير مسجل في جدول الطلبيات هذا مايخص ال Inner join لكن ماذا عن Left loin
تقوم ال left join بجلب كافة السجلات من الجدول الأيسر الذي هو جدول الزبائن CUSTOMER وجلب فقط السجلات المشتركة من الجدول الأيمن مع وضع قيمة NULL امام السجلات الغير مشتركة في هذه الحالة سيظهر اسم KHALED في الاستعلام وستضع امامه القيمة NULL للدلالة على ان هذا السجل غير مشترك عندها سنعرف نحن ان KHALED ليست لديه طلبية
الان سنقوم بصياغة استعلام على هذين الجدولين يوضح كيفية استخدام ال Left join
SELECT Customer.Cust_Name, Order.Order_ID
FROM Customer
LEFT JOIN Orders ONCustomer.Cust_ID = Order.CustID
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.