473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JOIN without excluding non-joined rows?

2 New Member
Good morning, folks. I'll get right to the point.

I have an MSSQL db with 3 tables. Table A holds users' data with logins and such. Table B holds workstations with machine names, hardware info, etc. Lastly, Table C holds record IDs from A and B in order to keep track of workstations paired to users. Some workstations don't have users yet, some users don't have workstations yet.

HOWEVER, I need to display all users WITH their workstations, but that same result set needs to include users which may not have a workstation associated with them

My current join query works beautifully, but does NOT include users with no workstations associated with them per Table C.

Table A is lan_user
id_user user_name user_login

Table B is lan_ws
id_ws ws_name ws_model operating_syste m

Table C is lan_userws_rel (as in, "relationships" )
id_userws_rel ws_id user_id


select lan_user.*, lan_userws_rel. *, lan_ws.* from lan_user INNER JOIN lan_userws_rel ON lan_user.id_use r = lan_userws_rel. user_id INNER JOIN lan_ws ON lan_ws.id_ws-lan_userws_rel. ws_id

Some additional information:
Workstations can have multiple users and vice versa, but I only really need one pairing in my result set - doesn't matter which one.
Mar 16 '07 #1
2 2421
Jogos
4 New Member
You should use LEFT JOIN but be aware when you will use a WHERE-clause on the 'not present' items
Info from books online (the guide in troubled moments!)

Joins can be categorized as:

Inner joins (the typical join operation, which uses some comparison operator like = or <>). These include equi-joins and natural joins.

Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same in both the students and courses tables.


Outer joins. Outer joins can be a left, a right, or full outer join.

Outer joins are specified with one of the following sets of keywords when they are specified in the FROM clause:


LEFT JOIN or LEFT OUTER JOIN

The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause, not just the ones in which the joined columns match. When a row in the left table has no matching rows in the right table, the associated result set row contains null values for all select list columns coming from the right table.


RIGHT JOIN or RIGHT OUTER JOIN

A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table.


FULL JOIN or FULL OUTER JOIN

A full outer join returns all rows in both the left and right tables. Any time a row has no match in the other table, the select list columns from the other table contain null values. When there is a match between the tables, the entire result set row contains data values from the base tables.
Mar 16 '07 #2
FuturShoc
2 New Member
{ :-O

Sweet mother of god. LEFT JOIN did essentially just what I needed.

This is proof that I have so, so much to learn about SQL. For the record, I really did look into this before posting, but I sometimes get lost in the examples given in my books.

THANK YOU.

Here was my final query:

select lan_user.*, lan_userws_rel. *, lan_ws.*
from lan_user
LEFT JOIN lan_userws_rel on lan_user.id_use r = lan_userws_rel. user_id
LEFT JOIN lan_ws on lan_ws.id_ws = lan_userws_rel. ws_id
ORDER BY id_user
Mar 16 '07 #3

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

Similar topics

3
10524
by: Peter L. Buschman | last post by:
I'm trying to think in Python, but am stumped here... What is the equivalent for the following if you are dealing with a tuple of non-strings? >>> import string >>> foo = ( '1', '2', '3' ) >>> string.join( foo, '.' ) '1.2.3'
46
2470
by: Leo Breebaart | last post by:
I've tried Googling for this, but practically all discussions on str.join() focus on the yuck-ugly-shouldn't-it-be-a-list-method? issue, which is not my problem/question at all. What I can't find an explanation for is why str.join() doesn't automatically call str() on its arguments, so that e.g. str.join() would yield "1245", and ditto for e.g. user-defined classes that have a __str__() defined. All I've been able to find is a 1999...
14
5725
by: mike | last post by:
I'm using postgresl 7.3.2 and have a query that executes very slowly. There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key of Item, and is also a field in LogEvent. Some ItemIDs in LogEvent do not correspond to ItemIDs in Item, and periodically we need to purge the non-matching ItemIDs from LogEvent. The query is: delete from LogEvent where EventType != 'i' and ItemID in
3
1691
by: Sandra-24 | last post by:
I'd love to know why calling ''.join() on a list of encoded strings automatically results in converting to the default encoding. First of all, it's undocumented, so If I didn't have non-ascii characters in my utf-8 data, I'd never have known until one day I did, and then the code would break. Secondly you can't override (for valid reasons) the default encoding, so that's not a way around it. So ''.join becomes pretty useless when dealing...
14
37378
by: Joe | last post by:
Does anyone know the difference, in practical terms, between Thread.Sleep (10000) and Thread.CurrentThread.Join (10000)?? The MSDN says that with Join, standard COM and SendMessage pumping continues, but what does this mean in practice for a typical Windows Forms or Windows Service application?? Some people say you should always use the latter.
5
2554
by: redstamp | last post by:
Try as I might I cannot find a way to write an access query to return a result set with the records from my database WITHOUT a certain set of values within a field. To explain, I have a table of customers linked to a table of customer contacts. The contact table has a field called 'type of contact'. Contact types can be numeric 1 to 40 and show the different types of contact I have with my customers (e.g. 1 - initial contact, 2 -...
7
5570
by: dunleav1 | last post by:
I have an application that uses the old join syntax instead of the SQL92 standards join syntax. I need to justify changing the code to the new standard. Is there any performance issue related to using the old syntax that are documented? Are there any other issues that I can use to justify a code upgrade?
0
1328
by: gr8white | last post by:
I'm running a query involving an outer join where one of the conditions is that the numeric value of a varchar field is between the numeric values of 2 varchar fields in another table (this has to do with an address range where in some cases the addresses include an alpha character). The query runs fine in the original database but for some reason it returns an "invalid number" error in another db against the exact same data, even though I am...
1
1445
by: Rich P | last post by:
Greetings, Left Join (outer join) queries are generally for excluding stuff. select t1.* from tblx t1 Left Join tbly t2 on t1.ID = t2.ID and t1.fld1 = t2.fld1 and t1.fld2 = t2.fld2... Where t2.ID Is Null And t1.fld1 = something and t1.date1 #somedate# ... You may want to perform your comparisons fisrt and the do a left join on
3
1652
by: ziycon | last post by:
I'm trying to do the below join, i have two tables as below. What i need is to get a list of all members from the 'members' table that have active set to 1 excluding any members in the admin_privileges table?? members: id(PK),uname,active admin_privileges: id(PK),uid(FK-members.id) SELECT members.id,members.uname,admin_pivileges.uid FROM members, admin_privileges WHERE members.active='1' AND members.id != admin_pivileges.uid
0
9704
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
10562
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
10319
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
10303
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
10070
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...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
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();...
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.