Domanda

Hey guys I am this close in being able to represent the data that I wish to display The column in question is a.state_Id. I understand from the research I have done that the a.State_Id is out of scope. What is the missing piece to my sql statement?

If I take out the a.State_ID = c.State_Id leaving only the b.Product_Id = c.Product_Id then the data is displayed but I need to match against the state and product.

I know i need to add in another join somewhere but im not sure how. If anyone could help me that would be awesome!

SELECT a.state_id, 
       a.state_name, 
       a.state_abbreviatedname, 
       b.product_id, 
       b.product_name, 
       c.stateproduct_price 
FROM   states a, 
       products b 
       LEFT OUTER JOIN stateproducts c 
ON a.state_id = c.state_id AND b.product_id = c.product_id 

Update 1

The states table has been populated and contains the following fields:

State_Id
State_Name
State_AbbreviatedName

The Products table which has been populated to contain the base products. This table has the following fields:

Product_Id
Product_Name

Each state will have the same products however the price for each product changes with each state. This is why i have the StateProducts table. This table is empty and will be populated one by one by an interface I have created. The statesproducts table will have the following fields

State_Id //reference/relational field to be user for comparison
Product_Id //reference/relational field to be user for comparison
StateProduct_Price //new field

so i understand that i will receive NULL values in the price column.

I have been able to return a sort of cartesion product of the States and products table. However I now need to append the price for each combination on the right side of this cartesion table. bearing in mind that stateproducts table is empty how would i accomplish this?

È stato utile?

Soluzione

I guess you have a Many-to-Many relationship in which States can have multiple Products and Products can be on different States. So you need to join States into the mapping table StateProducts so you can be able to get the products on table Products.

SELECT  a.State_ID, 
        a.State_Name, 
        a.State_AbbreviatedName, 
        b.Product_Id, 
        b.Product_Name, 
        c.StateProduct_Price
FROM    States a
        INNER JOIN StateProducts c
            ON a.State_ID = c.State_Id
        INNER  JOIN Products b 
            ON b.Product_Id = c.Product_Id

To further gain more knowledge about joins, kindly visit the link below:

UPDATE 1

use CROSS JOIN keyword,

SELECT a.state_id, a.state_name, a.state_abbreviatedname, b.product_id, b.product_name, c.stateproduct_price 
FROM   states a 
       CROSS JOIN products b 
       LEFT OUTER JOIN stateproducts c
          ON a.state_id = c.state_id AND 
             b.product_id = c.product_id

Altri suggerimenti

from A to C, C to B.

By doing LEFT JOIN for each, you'll still end up with all possible states regardless of having actual state products.

SELECT 
      a.State_ID, 
      a.State_Name, 
      a.State_AbbreviatedName, 
      b.Product_Id, 
      b.Product_Name, 
      c.Product_Price 
   FROM 
      States a
         LEFT  JOIN StateProducts c 
            ON a.State_ID = c.State_Id 
            LEFT JOIN Products b 
               ON  c.Product_Id = b.Product_Id

Try writing the from clause like this:

from StateProducts c join
     States a
     on c.state_id = c.state_id join
     Products b
     on c.product_id = b.product_id

For the Cartesian product (that is, to get all examples of states and products):

SELECT s.state_id, s.state_name, s.state_abbreviatedname, 
       p.product_id, p.product_name, 
       sp.stateproduct_price 
from States s cross join
     Products p left outer join
     StateProducts sp
     on sp.state_id = s.state_id and
        sp.product_id = p.product_id
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top