Unix Technical Forum

help with complex query

This is a discussion on help with complex query within the MySQL forums, part of the Database Server Software category; --> Hi, I'm having trouble with a complex query and was hoping to get some help. Here are my tables: ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:12 AM
cas
 
Posts: n/a
Default help with complex query

Hi,

I'm having trouble with a complex query and was hoping to get some
help.

Here are my tables:

Company
----------------
company_id | company_name | company_phone | company_county
--------------------------------------------------------------------------------------------------
1 | Joe's Garage | 555-1212 | Orange
2 | Sallys Shells | 555-1212 | Green
3 | Peters Pickles | 555-1212 | Orange
-------------------------------------------------------------------------------------------------

Salesman
--------------
salesman_id | salesman_name | regions
-----------------------------------------------------------------------
1 | Jane | 5
2 | Joey | 3
-----------------------------------------------------------------------


Regions
------------
region_id | county_name
-----------------------------------------------------------------------
11 | Orange
42 | Green
53 | Purple
-----------------------------------------------------------------------


Regions_lookup
-------------------------
salesman_id | region_id
-----------------------------------------------------------------------
1 | 11
1 | 53
2 | 11
2 | 42
-----------------------------------------------------------------------

Each salesman has to call some companies in their assigned regions
(counties). The assigned regions are stored in the Regions_lookup
table. I want to list out all companies in a given salesman's
region. What would be the MySQL query?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:12 AM
Jerry Stuckle
 
Posts: n/a
Default Re: help with complex query

cas wrote:
> Hi,
>
> I'm having trouble with a complex query and was hoping to get some
> help.
>
> Here are my tables:
>
> Company
> ----------------
> company_id | company_name | company_phone | company_county
> --------------------------------------------------------------------------------------------------
> 1 | Joe's Garage | 555-1212 | Orange
> 2 | Sallys Shells | 555-1212 | Green
> 3 | Peters Pickles | 555-1212 | Orange
> -------------------------------------------------------------------------------------------------
>
> Salesman
> --------------
> salesman_id | salesman_name | regions
> -----------------------------------------------------------------------
> 1 | Jane | 5
> 2 | Joey | 3
> -----------------------------------------------------------------------
>
>
> Regions
> ------------
> region_id | county_name
> -----------------------------------------------------------------------
> 11 | Orange
> 42 | Green
> 53 | Purple
> -----------------------------------------------------------------------
>
>
> Regions_lookup
> -------------------------
> salesman_id | region_id
> -----------------------------------------------------------------------
> 1 | 11
> 1 | 53
> 2 | 11
> 2 | 42
> -----------------------------------------------------------------------
>
> Each salesman has to call some companies in their assigned regions
> (counties). The assigned regions are stored in the Regions_lookup
> table. I want to list out all companies in a given salesman's
> region. What would be the MySQL query?
>


Untried, but something like:

SELECT company_name FROM Company
JOIN Regions on Company.company_county = Regions.county_name
JOIN Regions_lookup on Regions.region_id = Regions_lookup.region_id
JOIN Salesman on Regions_lookup.salesman_id = Salesman.salesman_id
WHERE Salesman.salesman_name = 'Jane';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:13 AM
cas
 
Posts: n/a
Default Re: help with complex query


> > I'm having trouble with a complex query and was hoping to get some
> > help.

>
> > Here are my tables: <snip>


> Untried, but something like:
>
> SELECT company_name FROM Company
> JOIN Regions on Company.company_county = Regions.county_name
> JOIN Regions_lookup on Regions.region_id = Regions_lookup.region_id
> JOIN Salesman on Regions_lookup.salesman_id = Salesman.salesman_id
> WHERE Salesman.salesman_name = 'Jane';


Yes, that worked like a charm! Thanks so much. In the past I would
have programmed it with some cumbersome looping and several database
queries. I am trying to learn how to code more efficiently.
Thanks again!
Carol

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:56 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com