|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 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 | +--------------------------+ |
|
#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:
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 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 Code:
PA.options_values_id = POV.products_options_values_id |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > Building Relationships w/ MySQL -- HELP!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|