473,804 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More JOIN Fun (Or is it?)

Hi there

Yes, I've read about JOINs, albeit after coding for a couple of years
already using queries like the following:

"SELECT m.LastName, m.FirstName, o.Address FROM members m, offices o WHERE
o.City='$vCity' AND m.Status<>'Reti red' AND m.Status<>'Susp ended' AND
o.MemberID=m.Me mberID ORDER BY LastName,FirstN ame"

($vCity comes from a drop-down list of city names)
Now... everything works just fine, and has for a long time.... but reading
this NG, and some online articles about JOINs has be wondering/perplexed...

What I am hoping for is someone who knows MySQL and is really bored to
perhaps explain why the above query is NOT a 'real' join (which i don't
think it is), and why that's necessarily BAD.

How can I take that query and turn it into a 'real' join, and more
importantly, why should I? What is 'wrong' with queries like the one
above?

Thanks - not interested in flames, etc, just someone who can take a bit of
time to explain....
Jul 23 '05 #1
2 1502
Good Man wrote:
"SELECT m.LastName, m.FirstName, o.Address FROM members m, offices o WHERE
o.City='$vCity' AND m.Status<>'Reti red' AND m.Status<>'Susp ended' AND
o.MemberID=m.Me mberID ORDER BY LastName,FirstN ame"

...perhaps explain why the above query is NOT a 'real' join (which i don't
think it is), and why that's necessarily BAD.
That looks like a real join to me, according to the SQL-89 language
specification. Prior to 1992, all joins followed that format.

Then people realized that this syntax didn't allow for more complex
joins. The most common of which was outer joins, where you want
everything from table1 even if there are no matching records in table2
(or vice versa). So Oracle and Microsoft and some others came up with
their own syntax extensions to SQL to handle these cases.

For example, you might see "(+)" used in Oracle expressions, to extend
comparison operators to mean, "field X must equal the following
expression, or field X must be absent (that is, NULL) because the outer
join matches no records from that field X's table".

The ANSI SQL committee that managed the SQL language defined more
standard syntax to support outer joins in their revision of the SQL
standard published in 1992. This might be what you're thinking of as a
"real" join. Of course the SQL-89 style joins are still valid for
backward compatibility, though they still can't represent outer joins
except through vendors' proprietary syntax.

The SQL-89 syntax is not necessarily bad. If inner joins are all you
need, then you can use either SQL-89 syntax or SQL-92 syntax with equal
results. Virtually all RDBMS vendors sooner or later implemented
support for the SQL-92 syntax. For this reason, you should be able to
mix SQL-89 style and SQL-92 (even in a single query, in some RDBMS
implementations ).
How can I take that query and turn it into a 'real' join, and more
importantly, why should I? What is 'wrong' with queries like the one
above?


Those queries are only "wrong" if you need to enhance the query to
support outer joins, and you want to comply with the ANSI/ISO SQL
standard instead of using proprietary syntax for the brand of RDBMS
you're using.

If you want to follow the SQL-92 syntax for joins, I'd rewrite the query
as follows:

SELECT m.LastName, m.FirstName, o.Address
FROM members AS m INNER JOIN offices AS o
ON m.MemberID = o.MemberID
WHERE o.City='$vCity' AND m.Status NOT IN ('Retired', 'Suspended')
ORDER BY m.LastName, m.FirstName

(Okay, even though it has nothing to do with rewriting the join syntax,
I couldn't help myself from using NOT IN. :-)

From that, it's a pretty simple alteration to make the query include
offices that have no members:

SELECT m.LastName, m.FirstName, o.Address
FROM members AS m RIGHT OUTER JOIN offices AS o
ON m.MemberID = o.MemberID
WHERE o.City='$vCity' AND m.Status NOT IN ('Retired', 'Suspended')
ORDER BY m.LastName, m.FirstName

Regards,
Bill K.
Jul 23 '05 #2
Bill Karwin <bi**@karwin.co m> wrote in
news:d1******** *@enews3.newsgu y.com:
Then people realized that this syntax didn't allow for more complex
joins. The most common of which was outer joins, where you want
everything from table1 even if there are no matching records in table2
(or vice versa).


I've come across this situation before and scratched my head.... hooray,
now I know the solution!

Thanks for your detailed explanation Bill.... much appreciated.

Regards,
GM
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8029
by: Fernand St-Georges | last post by:
Hi, can someone tell how to write a Trigger; I am familiar with Sybase Sql Anywhere trigger syntax. Actually I have three tables MEMBER, CONTRACT and PAYMENT I need to update the MEMBER.BALANCE once the PAYMENT.AMOUNT is INSERTED where PAYEMENT.CONTRAC_ID = CONTRACT.CONTRAC_ID and CONTRAT.MEMBER_ID = MEMBRE.MEMBER_ID I have more TRIGGERS to write, but with a good example it would be great
15
2737
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
4
1781
by: Bob Alston | last post by:
Some more, rather specific Access performance questions. IN a split front-end & back-end Access/Jet ONLY LAN situation, and with all query criteria fields and join fields indexed: 1. Is is good form to have a single query with base table with criteria joined to a related table - all in one query? Or should I do a two-step, first query does selection of main table and then join with other table? 2. I have a table with multiple...
4
1489
by: Iain King | last post by:
When I loop over one list I use: for item in items: print item but often I want to loop through two lists at once, and I've been doing this like I would in any other language - creating an index counter and incrementing it. For example, (completely arbitrary), I have two strings of the same length, and I want to return a string which, at each character
1
1457
by: KBuser | last post by:
Preface: I'm building an intranet site to build custom queries against our SQL Server (2000) db; The page is developed in ASP.net (2.0) with C# Codebehind. I dynamically generate and populate checkboxlists for each table that the user selects to query. I disable selecting non-related tables, and am in the process of creating inner joins when tables are related.... This is where I am getting stuck, here is the logic which builds the join...
3
2039
by: das | last post by:
Hello all, Can someone help me with this SQL? 1) EMPLOYEE table has columns 'employee_id' and 'emp_sid' and other columns 2) EMPLOYEE_BENEFITS table has a column called employee_entity, this column can be joined to either 'employee_id' OR 'emp_sid' but not both in the EMPLOYEE table.
6
4026
by: rshivaraman | last post by:
CREATE TABLE ( (10) NULL ) CREATE TABLE ( (10) NULL )
0
2074
by: kiran2nirvan | last post by:
hi please help in solving this i am recieving this error"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression." for the code CREATE procedure . (@frmMasterID int,
1
3281
by: nico3334 | last post by:
I have a query that currently pulls data from a main table and a second table using LEFT OUTER JOIN. I know how to do make another LEFT OUTER JOIN with the main table, but I want to add another LEFT OUTER JOIN to the second table. So I want the third table to be joined through the second table, not the main table. Here is my original code that joins the main table and the second table SELECT t1.supply, t2.inventory, FROM MAIN_TABLE...
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
9572
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,...
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...
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();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.