If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > joining 3 tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-13-11, 18:16
alski alski is offline
Registered User
 
Join Date: Dec 2002
Posts: 24
joining 3 tables

I have 3 tables:

'manager'
manager_id
manager_name

'manager_business'
manager_id
business_id

'business'
business_id
business_brand

If I type in the manager name, I would like to see which business_brands he/she manages.

I am halfway there with this statement:

SELECT manager_name, business_id
FROM manager
LEFT JOIN manager_business ON manager.manager_id = manager_business.manager_id
where manager_name like '%john%'

Any help?
Reply With Quote
  #2 (permalink)  
Old 09-13-11, 18:36
alski alski is offline
Registered User
 
Join Date: Dec 2002
Posts: 24
I may have answered my question, although there maybe a better way:

select manager_name, business_brand from manager, business, manager_business where manager.manager_id = manager_business.manager_id and manager_business.business_id = business.business_id and manager_name like '%john smith%'
Reply With Quote
  #3 (permalink)  
Old 09-14-11, 06:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
voila, the better way --
Code:
SELECT manager.manager_name
     , business.business_brand 
  FROM manager
INNER
  JOIN manager_business 
    ON manager_business.manager_id = manager.manager_id
INNER
  JOIN business
    ON business.business_id = manager_business.business_id
 WHERE manager.manager_name LIKE '%john smith%'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On