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:
  #1  
Old February 3rd, 2004, 04:33 PM
bsmith bsmith is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: palm bay, fl, usa
Posts: 204 bsmith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to bsmith
Building Relationships w/ MySQL -- HELP!!

I have three tables, and am looking to build a query to pulls information from all three of them. I unsucessfully tried once, and pulled over 10k records some how, when I only need 176 records.

From the tables below, I am trying to build a form that as the following data:

Attribute ID -- Product Model -- Option Name --- Option Price.

I am needing it to come from:

products_attributes
products_attributes_id
options_values_price

products_options_values
products_options_values_name

products
products_model

Here are my relations:
products_options_values.products_options_values_id
-> products_options_values.products_options_values_id

products_attributes.products_id
-> describe products.products_id

I believe using just those two are enough to build my query, but when I tried with this query (I know it is probably poorly written in more then one way, so please be be nice).

select products.products_model as itemName,
products_options_values.products_options_values_na me,
products_attributes.options_values_price as attributePrice,
products_attributes.products_attributes_id as attributeID
from products_attributes, products_options_values, products
where products_attributes.options_values_price != "0.0000" and
products_options_values.products_options_values_id
= products_options_values.products_options_values_id ;

That returns 10k records , and of course, should not. Any assistance would greatly be appreciated!

mysql> describe products_attributes;
+------------------------+
| Field |
+------------------------+
| products_attributes_id |
| products_id |
| options_id |
| options_values_id |
| options_values_price |
| price_prefix |
+------------------------+

mysql> describe products_options_values;
+------------------------------+
| Field |
+------------------------------+
| products_options_values_id |
| language_id |
| products_options_values_name |
+------------------------------+

mysql> describe products;
+--------------------------+
| Field |
+--------------------------+
| products_id |
| products_model |
+--------------------------+

Reply With Quote
  #2  
Old February 3rd, 2004, 05:09 PM
brewthatistrue brewthatistrue is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: CA, USA
Posts: 277 brewthatistrue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 sec
Reputation Power: 2
RE: Building Relationships w/ MySQL -- HELP!!

i'm not sql expert, but here's my try.

also, a few things to note.
Aliases are supposed to help readability - make them short.
Another readability tip, capitalize your SQL keywords.
Don't be afraid of whitespace (especially when trying things out - when it works you can go back and remove whitespace)

this line:
Quote:
products_options_values.products_options_values_id
= products_options_values.products_options_values_id
this line does nothing to limit your results. it's like saying WHEN 1 = 1;

here's my sql code, i'm not 100% confident that it will work though.
it's a just a select statement, no data loss if it fails. :p
(i have some comments in here, so remove them when using the code)
Code:
SELECT
PA.products_attributes_id AS attributeID,
P.products_model AS productModel, //productModel was itemName in your sql
POV.products_options_values_name AS optionName,
PA.options_values_price AS optionPrice //optionPrice was attributePrice in your sql

FROM 
products_attributes AS PA, 
products AS P, 
products_options_values AS POV


WHERE
products_attributes.options_values_price != "0.0000" AND
PA.products_id = P.products.id AND
P.options_values_id = POV.products_options_values_id

Reply With Quote
  #3  
Old February 3rd, 2004, 05:50 PM
bsmith bsmith is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: palm bay, fl, usa
Posts: 204 bsmith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to bsmith
RE: Building Relationships w/ MySQL -- HELP!!

brewthatistrue,

Thank you very much. I had to make a couple little changes to your statement, but in the end, it works just fine (I think) ...

Thank you again,

Brian

SELECT
PA.products_attributes_id AS attributeID,
P.products_model AS productModel,
POV.products_options_values_name AS optionName,
PA.options_values_price AS optionPrice

FROM
products_attributes AS PA,
products AS P,
products_options_values AS POV


WHERE
PA.options_values_price != "0.0000" AND
PA.products_id = P.products_id AND
PA.options_values_id = POV.products_options_values_id

Reply With Quote
  #4  
Old February 3rd, 2004, 06:03 PM
brewthatistrue brewthatistrue is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: CA, USA
Posts: 277 brewthatistrue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 sec
Reputation Power: 2
RE: Building Relationships w/ MySQL -- HELP!!

great!
ah, i see.

in the WHEN statement i forgot to use the alias on this line

Code:
products_attributes.options_values_price != "0.0000" AND
should be
Code:
PA.options_values_price != "0.0000" AND


and used the wrong alias on this line
Code:
P.options_values_id = POV.products_options_values_id
should be
Code:
PA.options_values_id = POV.products_options_values_id

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Building Relationships w/ MySQL -- HELP!!


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


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





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