Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Dell PowerEdge Servers
  #1  
Old January 8th, 2008, 02:14 PM
bigbavarian bigbavarian is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 42 bigbavarian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 29 m 18 sec
Reputation Power: 2
MySQL 4 - Joining Multiple Tables

I am working on a beer website that has tables for members, breweries, beers, beer reviews, etc. It allows a member to add brewery and beer infromation as well as reviews or the beers.

I am trying to create a page that shows what they have added to the site and I am trying to create one query instead of three (reviews, beers added, breweries added).

I am really new to joining tables and can use some assistance as I cannot seem to get the query to work. Here is what I have right now.

SELECT 'members.UserID', 'members.CreateDate', 'members.City', 'members.State', 'members.Country', 'beerreviews.comments', 'beerreviews.rating', 'beerreviews.review_date', 'beerreviews.fk_user_id', 'beerreviews.fk_beer_id', 'beerreviews.beer_review_id', 'beerstable.BeerID', 'beerstable.BeerName', 'beerstable.BreweryID', 'beerstable.UserID' AS 'BeersUserID', 'beerstable.BeerCreateDate', 'brewerytable.BreweryID', 'brewerytable.BreweryName', 'brewerytable.BreweryCreateDate', 'brewerytable.BreweryCountry', 'brewerytable.UserID' AS 'BreweryUserID'
FROM 'beerstable'
LEFT JOIN 'members' on ('beerstable.BeersUserID' = 'members.UserID')
LEFT JOIN 'beerreviews' on ('beerstable.BeerID' = 'beerreviews.fk_beer_id')
LEFT JOIN 'brewerytable' on ('beerstable.BreweryID' = 'brewerytable.BreweryUserID')
WHERE 'members.UserID' = '$UserID'

Here are the fields from the tables I want to use...

Table 1: "members"

UserID
CreateDate
City
State
Country


Table 2: "beerreviews"

beerreviews.comments
beerreviews.rating
beerreviews.review_date
beerreviews.fk_user_id
beerreviews.fk_beer_id
beerreviews.beer_review_id


Table 3: "beerstable"

beerstable.BeerID
beerstable.BeerName
beerstable.BreweryID
beerstable.UserID
beerstable.BeerCreateDate


Table 4: "brewerytable"

brewerytable.BreweryID
brewerytable.BreweryName
brewerytable.BreweryCreateDate
brewerytable.BreweryCountry
brewerytable.UserID

Field Relationships...
members.UserID = beerstable.UserID = brewerytable.UserID = beerreviews.fk_user_id
beerstable.BeerID - beerreviews.beer_review_id
beerstable.BreweryID - Brewerytable.BreweryID

Thank You for your time,

Stephen

Reply With Quote
  #2  
Old January 9th, 2008, 06:41 AM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,719 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 4 m 29 sec
Reputation Power: 6
What exactly is the problem you are facing with what you have?

First thing I see is that you have
Quote:
LEFT JOIN 'members' on ('beerstable.BeersUserID' = 'members.UserID')

but later say the relationship is
Quote:
members.UserID = beerstable.UserID

2 Separate linkages - which is correct?

Same goes for
Quote:
LEFT JOIN 'beerreviews' on ('beerstable.BeerID' = 'beerreviews.fk_beer_id')

with
Quote:
beerstable.BeerID - beerreviews.beer_review_id


You last join doesn't match up either... so which is right?
__________________
life is a game.... Have fun
-----------------------------
http://www.phpwomen.org
strength in unity

PHPCommunity IRC
#phpc on freenode

Reply With Quote
  #3  
Old January 9th, 2008, 10:21 AM
bigbavarian bigbavarian is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 42 bigbavarian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 29 m 18 sec
Reputation Power: 2
For your first observation, I used the term 'beerstable.BeersUserID' because in the Select line I thought I was changing the field to 'beerstable.UserID' AS 'BeersUserID'. Maybe this is where I am going wrong. Since UserID is in a couple tables I thought I have to make them different.

Should I leave the code as:
LEFT JOIN 'members' on ('beerstable.UserID' = 'members.UserID')

On the second one. You found part of the problem. I used beer instead of fk in fk_review_id.

There I go again trying to get another beer in there.

It looks like we are closer. I will try a couple changes and update the post again.

Thank You,

Stephen

Reply With Quote
  #4  
Old January 9th, 2008, 10:37 AM
bigbavarian bigbavarian is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 42 bigbavarian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 29 m 18 sec
Reputation Power: 2
I am back. Still no luck.

Here is the query I changed it to...
SELECT 'members.UserID', 'members.CreateDate', 'members.City', 'members.State', 'members.Country', 'beerreviews.comments', 'beerreviews.rating', 'beerreviews.review_date', 'beerreviews.fk_user_id', 'beerreviews.fk_beer_id', 'beerreviews.beer_review_id', 'beerstable.BeerID', 'beerstable.BeerName', 'beerstable.BreweryID', 'beerstable.UserID' AS 'BeersUserID', 'beerstable.BeerCreateDate', 'brewerytable.BreweryID', 'brewerytable.BreweryName', 'brewerytable.BreweryCreateDate', 'brewerytable.BreweryCountry', 'brewerytable.UserID' AS 'BreweryUserID'
FROM 'beerstable'
LEFT JOIN 'members' on ('beerstable.UserID' = 'members.UserID')
LEFT JOIN 'beerreviews' on ('beerstable.BeerID' = 'beerreviews.fk_beer_id')
LEFT JOIN 'brewerytable' on ('beerstable.BreweryID' = 'brewerytable.UserID')
WHERE 'members.UserID' = '$UserID'

Here is the error I am receiving...

#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''beerstable' LEFT JOIN 'members' on ( 'beerstable.UserID' = 'm


fk_beer_id is correct and the typo was in the relashionship section.

Reply With Quote
  #5  
Old January 9th, 2008, 11:58 AM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,719 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 4 m 29 sec
Reputation Power: 6
Ok - I had to clean things up a bit so I understood what was going on - it makes things easier for me to read and understand (missed those AS because of it). Ok - lets try this ( it removes the quotes on the table names).

Code:
SELECT 'm.UserID', 
	'm.CreateDate', 
	'm.City', 
	'm.State', 
	'm.Country', 
	'br.comments', 
	'br.rating', 
	'br.review_date', 
	'br.fk_user_id', 
	'br.fk_beer_id', 
	'br.beer_review_id', 
	'bt.BeerID', 
	'bt.BeerName', 
	'bt.BreweryID', 
	'bt.UserID' AS 'BeersUserID', 
	'bt.BeerCreateDate', 
	'brew.BreweryID', 
	'brew.BreweryName', 
	'brew.BreweryCreateDate', 
	'brew.BreweryCountry', 
	'brew.UserID' AS 'BreweryUserID'
FROM beerstable bt
LEFT JOIN members m on (bt.UserID = m.UserID)
LEFT JOIN beerreviews br on (bt.BeerID = br.fk_beer_id)
LEFT JOIN brewerytable brew on (bt.BreweryID = brew.UserID)
WHERE m.UserID = '$UserID'


I personally don't quote table names - if I have to I use the mysql specific backtick instead. IIRC you need to have the SQLMODE set to ANSI_QUOTES for it to work with quotes - but I can definitely be wrong on that.

If that doesn't work - the next thing I would try is moving that last alias from the last position to somewhere else - even if it is just one above.

Reply With Quote
  #6  
Old January 9th, 2008, 05:04 PM
bigbavarian bigbavarian is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 42 bigbavarian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 29 m 18 sec
Reputation Power: 2
Lig,

I have been thinking through this all day and I am wondering if the results will even provide what I am looking for. I think I will have to create seperate queries for each. I am not sure it can all be done with one query, At least with my knowledge. Then afgter I have completed it I can show it to someone and see it they can streamline it a little.

I did try your query and it gave me funky results. Perhaps you are corect about changing the mode.

I will try the changes and present them again when I am done.

I thank you for all your help,

Stephen

Reply With Quote
  #7  
Old January 9th, 2008, 05:16 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,719 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 4 m 29 sec
Reputation Power: 6
so it removed the syntax error? if so then the original problem is resolved and now we need to figure out how to get the data you wish.

Feel free to make the individual queries and we can work on joining them afterwards.

Reply With Quote
  #8  
Old January 9th, 2008, 07:20 PM
bigbavarian bigbavarian is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 42 bigbavarian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 29 m 18 sec
Reputation Power: 2
Yes the syntax error is gone. Yeah. I have started on small left join queries and will work my way up slowely.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > MySQL 4 - Joining Multiple Tables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway