الانتقال إلى المحتوى

لماذا هذا الكود لايعطى نتيجة


mahmoud shoman

Recommended Posts

select employee.ename
from emp employee
where employee.empno not in
(select manager.mgr from emp manager )

على الرغم ان الكود هذا يعطى نتيجة

select employee.ename
from emp employee
where employee.empno in
(select manager.mgr from emp manager )

رابط هذا التعليق
شارك

Because all emp_id's are not in nothing



The above SQL statement attempts to display all the employees who do not have any subordinates. Logically, this SQL statement should have returned eight rows. However, the SQL statement does not return any rows. One of the values returned by the inner query is a null value and hence the entire query returns no rows. The reason is that all conditions that compare a null value result in a null. So whenever null values are likely to be part of the resultant set of a subquery, do not use the NOT IN operator. The NOT IN operator is equivalent to !=ALL.
Notice that the null value as part of the resultant set of a subquery will not be a problem if you are using the IN operator. The IN operator is equivalent to =ANY

رابط هذا التعليق
شارك

السلام عليكم ورحمة الله

بالاضافة الى ما ذكرة الأخ / DR.Lava

يمكنك استخدام ال nvl للتعامل مع ال null value

SQL> select employee.ename
 2  from emp employee
 3  where employee.empno not in
 4  (select nvl(manager.mgr,0) from emp manager )
 5  /

ENAME
----------
SMITH
ALLEN
WARD
MARTIN
TURNER
ADAMS
JAMES
MILLER

8 rows selected.

رابط هذا التعليق
شارك

كل الكلام السابقى كلام سليم 100%
ولكن الافضل والاصح واللى اوراكل نفسه بستعمله فى الابكليشن بتاعها
هو استخدام exist or no exist
وذلك فى الحقول التى يمكن ان تحتفظ بقيمة NULL
بمعنى الكود يكون

select employee.ename 
from emp employee 
where exist (selectmanager.mgr from emp manager  where emp_no =selectmanager.mgr ) 

رابط هذا التعليق
شارك

انضم إلى المناقشة

يمكنك المشاركة الآن والتسجيل لاحقاً. إذا كان لديك حساب, سجل دخولك الآن لتقوم بالمشاركة من خلال حسابك.

زائر
أضف رد على هذا الموضوع...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   تمت استعادة المحتوى السابق الخاص بك.   مسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

جاري التحميل
×
×
  • أضف...

برجاء الإنتباه

بإستخدامك للموقع فأنت تتعهد بالموافقة على هذه البنود: سياسة الخصوصية