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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing MYSQL queries.

Status
Not open for further replies.

Chelsea7

Programmer
Aug 25, 2008
69
US
Hello everyone,

I'm using MYSQL Server version: 4.1.14 and I'm trying to execute a query that has more than 4 tables. The purpose is to produce a mailing list and download it into excel. The excel part is fine. But for some reason, I can't go beyond 3 tables in the code below;
===============================

$result=mysql_query("select * from name left join position ON name.id=position.id left join address ON position.id=address.id left join company ON address.id=company.id");

================================

If I do another LEFT JOIN, the query comes up blank and no data appears in the excel spreadsheet.

I'm trying to add the following;

LEFT JOIN email_address ON address.id=email_address.id

Any assistance would be appreciated.

Chelsea

 
Do you get any errors? What happens if you issue the same query from a front-end application?

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
something is definitely wonky, and i think it's because you're joining all those tables on their ids

for instance, how likely is it that the name with id 42 is going to match the position with id 42? if it does, that means that position 42 won't match any other name, otherwise you'd have two names with id 42

and then what are the chances that position 42 matches address 42? and company 42? see where i'm going with this?

here's what your query should look like instead --

select ...
from name
left
join position
on position.id = name.position_id
left
join address
on address.id = position.address_id
left
join company
on company.id = address.company_id

see the difference?

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top