473,722 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inner Join Not Exists

5 New Member
Hey All,

I am having a problem retreiving all records that are not in a second table..

I want to SELECT all records from table1 where table1.ID is not in table2

[Using MICROSOFT SQL SERVER 2005]

Here is my current code.
Expand|Select|Wrap|Line Numbers
  1. SELECT table1.ID_PK, table1.fName, table1.lName
  2. FROM table1 INNER JOIN
  3.      table2 ON table2.assoc = '17058' AND table2.status = 'APPROVED' AND table1.ID_PK <> table2.ID_FK
  4. WHERE (table1.status <> 'PENDING')
  5.  
This results in only retreving some records from table1. It succesfully does nto return the 1 record from table1 that is in table2 but it should return many more records from table1

I have been using SQL for some time, but usually for basic things, I have just started using more advanced queries so forgive my newbyism... I assume I need a NOT EXISTS statement, but when I tried several variations with that I got less then steller results.

FYI, in this example there is only 1 record in table2, but any suggestions should keep in mind(if possible) that the total records in table2 will probably be a min of 5000

Any help would be greatly appreciated!

Thanks.
Apr 6 '07 #1
4 16600
iburyak
1,017 Recognized Expert Top Contributor
Try this:


[PHP]SELECT ID_PK, fName, lName
FROM table1 t1
WHERE status <> 'PENDING'
AND NOT EXISTS (select * from table2 where assoc = '17058' AND status = 'APPROVED' and ID_PK = t1.ID_PK )[/PHP]

Good Luck.
Apr 6 '07 #2
Midnight Marauders
5 New Member
That worked perfectly.. nice.

Thanks much!!

Try this:


[PHP]SELECT ID_PK, fName, lName
FROM table1 t1
WHERE status <> 'PENDING'
AND NOT EXISTS (select * from table2 where assoc = '17058' AND status = 'APPROVED' and ID_PK = t1.ID_PK )[/PHP]

Good Luck.
Apr 6 '07 #3
Midnight Marauders
5 New Member
Now what if I want to return all records from Table1 and I want the Table2.status value if Table1.ID_PK = Table2.ID_FK, if it doesnt i still want the record from Table1 it just wont have a Table2.status value

It seems with my code that it is pulling duplicate records. I think the output is correct asside from the duplication of course.. Nope, as i am writing this and looking at the query results I see the table2 returned values are not correct, so is this where I would use a RIGHT OUTER JOIN??

My Current Code:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM Table1 CROSS JOIN Table2
  3.  
Any help you could provide would be greatly appreciated!!

Thanks..
Apr 6 '07 #4
iburyak
1,017 Recognized Expert Top Contributor
Try this:

[PHP]Select distinct *
FROM Table1
LEFT JOIN Table2 ON Table1.ID_PK = Table2.ID_FK[/PHP]


Good Luck.
Apr 7 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3353
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four inner joins, as follows : SELECT DISTINCT upcards.id,statuskey.status,upcards.firstname,upcards.lastname,originkey.ori gin,associatekey.username,associatekey2.username,upcards.deleted FROM upcards,status,origins,associates INNER JOIN status...
3
6414
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
3
59074
by: indabert | last post by:
Hello, I have two tables (table1 and table2). I want to set a flag in table1 for each common row with table2. I use the following syntax UPDATE table1 SET flag='Y' from table1 INNER JOIN table2 on (table1.a = table2.a) AND (table1.b = table2.b) However the situation arises where I may have a row in table2 that matches with two or more rows in table1. The requirement is that I only want to set the flag on a single row in table1.
4
21432
by: PASQUALE | last post by:
Hi I have a question: do the both statements below give the same result? If yes then does somebody know something about preformance differencies using these joins? SELECT A.* FROM Table1 A INNER JOIN Table2 B on A.Field1 <> B.Field1
23
82033
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the following: SELECT supplier_name FROM supplier
6
9314
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: tblEmployees empId -- EmpName -- EmpRole -- EmpManager -------....------------.... ---------....--------------- 1........ dan yella..........1..........2
52
6332
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
12
13189
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
11
19968
by: YZXIA | last post by:
Is there any difference between explicit inner join and implicit inner join Example of an explicit inner join: SELECT * FROM employee INNER JOIN department ON employee.DepartmentID = department.DepartmentID
0
8867
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8740
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9158
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2606
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.