473,408 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

select join return duplicate result

I have to table, first column of each table is primary key

Table1
User_Id | User_Password | User_StaffNo
---------------------------------------
1234 | pass | 1234
1235 | pass | 1235
1236 | pass | 1236

Table2
Table2_Id | User_Id | Comp_Id
----------------------------------
1 | 1234 | 2
2 | 1235 | 2

i want to query for the row that exist in Table1 but not in Table2.
my query = select Table1.User_StaffNo from Table1,Table2 where Table1.User_Id<>Table2.User_Id

the query returned one row 1234, one row 1235 and two 1236 which is as below:

User_StaffNo
---------------
1234
1235
1236
1236

What i wanted it to return is just one 1236, like this:

User_StaffNo
---------------
1236

What is wrong here?
Mar 20 '08 #1
2 1868
amitpatel66
2,367 Expert 2GB
I have to table, first column of each table is primary key

Table1
User_Id | User_Password | User_StaffNo
---------------------------------------
1234 | pass | 1234
1235 | pass | 1235
1236 | pass | 1236

Table2
Table2_Id | User_Id | Comp_Id
----------------------------------
1 | 1234 | 2
2 | 1235 | 2

i want to query for the row that exist in Table1 but not in Table2.
my query = select Table1.User_StaffNo from Table1,Table2 where Table1.User_Id<>Table2.User_Id

the query returned one row 1234, one row 1235 and two 1236 which is as below:

User_StaffNo
---------------
1234
1235
1236
1236

What i wanted it to return is just one 1236, like this:

User_StaffNo
---------------
1236

What is wrong here?
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT user_staffno from table1 WHERE user_id NOT IN (SELECT user_id FROM table2)
  3.  
  4.  
Mar 20 '08 #2
mwasif
802 Expert 512MB
Or try the following
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.User_StaffNo from Table1
  2. LEFT JOIN Table2 ON Table1.User_Id=Table2.User_Id
  3. WHERE Table2.User_Id IS NULL
Mar 20 '08 #3

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

Similar topics

1
by: Phil Powell | last post by:
Here is the scope of what I need to do; want: enrollment_year allowed (even if null) all of ica criteria:
9
by: atse | last post by:
Hi, My table in the database may contain duplicate records, which means except the primary key (auto_increment) field is unique, all or almost of all the fields are with the same content. How...
5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
2
by: shumaker | last post by:
The query: SELECT BTbl.PKey, BTbl.Result FROM BTbl INNER JOIN ATbl ON BTbl.PKey = ATbl.PKey WHERE (ATbl.Status = 'DROPPED') AND (BTbl.Result <> 'RESOLVED') Returns no rows. If...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
6
by: Eric Robinson | last post by:
Hi all, I'm having real trouble wrapping my newbie brain around this problem. Can someone please tell me the most efficient (or any!) way to write a SELECT statement to return a set of rows from...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
17
by: bobo420 | last post by:
Hi! I have 4 tables (table1, table2, table3, table 4) I need to do select * from all four table and get them sorted all the tables have field named id, so I figured that's the field I should...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.