Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two Tables Join...

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA

I have a very easy question for you guys. I want to join two tables in order to get all records with City = Burlington. (See Data below)

Code:
_USER_
ID|NAME|CITY|USER_ID
233|Bob|Burlington|B122
225|Rick|Los Angeles|B666
656|Nestor|Maui|B877

Code:
_MEMBERS_
ID|NAME|CITY|USER_ID
9003|Andy|Florida|B643
8700|Kris|Miami|B739
3302|Bob|New Jersey|B914
2200|Andre|Florida|B454
9882|Ricky|Burlington|B766
3222|Bob|Burlington|B122

Code:
SELECT
	USERS.*,
	MEMBERS.*
FROM
	USERS,
	MEMBERS
WHERE
	USERS.CITY = 'Burlington'
AND 
	MEMBERS.CITY = 'Burlington'

I'm looking for the following results. I would eventually use a SELECT DISTINCT to not have any duplicate results.
Code:
_QUERY RESULTS_	
ID|NAME|CITY|USER_ID
233|Bob|Burlington|B122
9882|Ricky|Burlington|B766
3222|Bob|Burlington|B122


Thanks.
 
You don't need JOINS (read the link below for more on JOINS), you need a UNION:

Code:
SELECT * FROM USER WHERE CITY = "Burlington"
UNION
SELECT * FROM MEMBERS WHERE CITY = "Burlington"

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for database developers:
The Fundamentals of Relational Database Design
Understanding SQL Joins
 
Is the _user_ 233|Bob|Burlington|B122 The same as the _members_ 3222|Bob|Burlington|B122? If they are, then your tables are not normalized. You're repeating the City field in two tables. That's a no-no.
 
I decided not to go into the normalization issues....mine was more the name field than the city field.....I just answered the question!

Les
 
I just answered the question
I'm not sure, as Members.ID is different from Users.ID ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top