473,394 Members | 1,663 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,394 software developers and data experts.

Whats a union?


Before anyone cracks a quick joke... my question does not refer to
same-sex marriage...

I know how to create a join - correct me if I am wrong, but its
something like the following:

SELECT contacts.firstname,contacts.lastname,address.line_ 1
FROM contacts,address
WHERE contacts.address_hash='$myhashkey'
AND address.hash='$myhashkey';

I have seen mixed references in my books about unions and joins that
make me think they are in some way related. My Core MySQL book, which I
think is great, is unfortunately not clear enough for my head to grasp.

I'd appreciate if someone would could give me a real and an imaginery
world example (thus sample code and perhaps compare apples and oranges
in a shopping list or whatever)...

and... ahem... if I'm wrong about my understanding for a 'join' then
perhaps you could clarify that too for me...

I've got about a years MySQL behind me but very little of it is
commercial/work related so I appreciate all the help/direction you can
give... Please reply to the newsgroup for others to learn from...

Cheers
Randell D.
Jul 23 '05 #1
2 1509
To correct your misconception
Joins will join one table to another and what you have provided is an
example that produces a reult set similar to that of a join but not strictly
a join (There are perfomance and other issues with your SQL syntax.

O.K.
Lesson 1.
Inner and outer joins and know your lefts and rights!

SELECT e.EmployeeName, c.CarModel from Employee e, EmployeeCars c
JOIN c.EmployeeID = e.EmployeeID
WHERE e.EmployeeSalary > 20000

This is an implied inner join that will return all employees regardless of
whether or not they have a company car. that earn over £20,000 a year! The
CarModel field will contain nulls if no car is attached to the employee
table it could also contain nulls if there is an entry for an employee in
the car table but the field has not been set! (Unless NULLS are not allowed
for this field) Confused? You will be!

Replace the word JOIN with LEFT OUTER JOIN and you will only get employees
who have an employee
entry in the car table that earn over 20k! - very useful!

It's late, I'm tired and I don't fancy writing an SQL manual right now so
I'll leave others to explain unions! Also consider what would happen if an
employee had more than one company car and you wanted a list of all
employees who had Mondeos! How would you stop the employee appearing twice!

There are loads of issues with joins and you really should read up on them!

Check out
http://www.theregister.co.uk/2004/05...r_a_new_skill/
and http://www.w3schools.com/sql/default.asp

Regards

James West
"Randell D." <re******************************@fiprojects.moc > wrote in
message news:d7bRd.416494$8l.121589@pd7tw1no...

Before anyone cracks a quick joke... my question does not refer to
same-sex marriage...

I know how to create a join - correct me if I am wrong, but its something
like the following:

SELECT contacts.firstname,contacts.lastname,address.line_ 1
FROM contacts,address
WHERE contacts.address_hash='$myhashkey'
AND address.hash='$myhashkey';

I have seen mixed references in my books about unions and joins that make
me think they are in some way related. My Core MySQL book, which I think
is great, is unfortunately not clear enough for my head to grasp.

I'd appreciate if someone would could give me a real and an imaginery
world example (thus sample code and perhaps compare apples and oranges in
a shopping list or whatever)...

and... ahem... if I'm wrong about my understanding for a 'join' then
perhaps you could clarify that too for me...

I've got about a years MySQL behind me but very little of it is
commercial/work related so I appreciate all the help/direction you can
give... Please reply to the newsgroup for others to learn from...

Cheers
Randell D.

Jul 23 '05 #2
J West wrote:
To correct your misconception
Joins will join one table to another and what you have provided is an
example that produces a reult set similar to that of a join but not strictly
a join (There are perfomance and other issues with your SQL syntax.

O.K.
Lesson 1.
Inner and outer joins and know your lefts and rights!

SELECT e.EmployeeName, c.CarModel from Employee e, EmployeeCars c
JOIN c.EmployeeID = e.EmployeeID
WHERE e.EmployeeSalary > 20000

This is an implied inner join that will return all employees regardless of
whether or not they have a company car. that earn over £20,000 a year! The
CarModel field will contain nulls if no car is attached to the employee
table it could also contain nulls if there is an entry for an employee in
the car table but the field has not been set! (Unless NULLS are not allowed
for this field) Confused? You will be! Okay... I can understand the above...

Replace the word JOIN with LEFT OUTER JOIN and you will only get employees
who have an employee
entry in the car table that earn over 20k! - very useful!

It's late, I'm tired and I don't fancy writing an SQL manual right now so
I'll leave others to explain unions! Also consider what would happen if an
employee had more than one company car and you wanted a list of all
employees who had Mondeos! How would you stop the employee appearing twice!
But I'll have to play with my LEFT OUTER JOINs to fully
understand/apprceciate them...

There are loads of issues with joins and you really should read up on them!

Check out
http://www.theregister.co.uk/2004/05...r_a_new_skill/
and http://www.w3schools.com/sql/default.asp

Regards

James West


Thanks for the help and refered links and sleep well ;-)

randelld
Jul 23 '05 #3

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

Similar topics

2
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a...
5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
4
by: Girish | last post by:
I have 2 differesnt defination of same Union as below and a piece of code for printing size of Union and its members.. union U { union U { int i; int j; }a;
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.