Link to home
Start Free TrialLog in
Avatar of running32
running32

asked on

Problem with select statement

I have a select statement and each time I try to select I get differenct errors.

select org.name, loc.address1, loc.address2, loc.city, loc.state, loc.zip,phones.phone from locations as loc, organizations as org , phones
inner join organizationlocations as orgloc on org.organizationid = orgloc.organizationid
inner join locations as loc1 on orgloc.locationsid = loc1.locationid

The one I am getting at the moment is

 Line 1
The column prefix 'org' does not match with a table name or alias name used in the query.

Thanks
Avatar of ABaruh
ABaruh

select org.name, loc.address1, loc.address2, loc.city, loc.state, loc.zip,phones.phone from locations loc, organizations org , phones
inner join organizationlocations orgloc on org.organizationid = orgloc.organizationid
inner join locations loc1 on orgloc.locationsid = loc1.locationid
woah, you are joining locations back to itself?  why?  your query should be:

select org.name, loc.address1, loc.address2, loc.city, loc.state, loc.zip,phones.phone from locations loc, organizations org , phones
inner join organizationlocations orgloc on org.organizationid = orgloc.organizationid
and orgloc.locationsid = loc1.locationid
SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of running32

ASKER

I guess my query does not work at all.

What I need is the name, address from the tables organization and locations.

These tables are linked by a locationid.  organization table has organizationid and organizationlocations table has organizationid and locationid.  Then locations table has locations id.  I need to join up the 3 tables so that I can pull the information.  

Thanks for your help
select
     org.name
     , loc.address1
     , loc.address2
     , loc.city
     , loc.state
     , loc.zip
     , phones.phone
from
     locations loc
inner join
     organizationlocations orgloc on
          loc.locationid = orgloc.locationid
inner join
     organizationlocations orgloc on
          org.organizationid = orgloc.organizationid
inner join
     phones on
          loc.locationid = phones.locationid
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks guys for answering back so quickly I really appricate it.