2017-07-28 15 views
1

私は2つのテーブルを持っています:製品とメタ。別のテーブルと自己結合テーブルを結合します

製品テーブル:

+----+----------+ 
| id | name  | 
+----+----------+ 
| 1 | TV  | 
| 2 | Computer | 
| 3 | Freezer | 
+----+----------+ 

メタテーブル:

+----+------------+-----------+------------+ 
| id | product_id | meta_key | meta_value | 
+----+------------+-----------+------------+ 
| 1 |   1 | currency | USD  | 
| 2 |   1 | price  | 1100  | 
| 3 |   2 | currency | PLN  | 
| 4 |   2 | price  | 9300  | 
| 5 |   3 | currency | USD  | 
| 6 |   3 | price  | 1200  | 
+----+------------+-----------+------------+ 

今、次のクエリは正常に動作します:

select price.product_id, products.name, price.meta_value as 'price', currency.meta_value as 'currency' 
from meta as price 
join meta as currency on(price.product_id=currency.product_id and currency.meta_key='currency') 
join products on(products.id=price.product_id) 
where price.meta_key='price'; 

結果:

+------------+----------+-------+----------+ 
| product_id | name  | price | currency | 
+------------+----------+-------+----------+ 
|   1 | TV  | 1100 | USD  | 
|   2 | Computer | 9300 | PLN  | 
|   3 | Freezer | 1200 | USD  | 
+------------+----------+-------+----------+ 

しかし、問合せ:

select price.product_id, products.name, price.meta_value as 'price', currency.meta_value as 'currency' 
from meta as price, meta as currency 
join products on(products.id=price.product_id) 
where 
    price.product_id=currency.product_id 
    and price.meta_key='price' 
    and currency.meta_key='currency'; 

を返す: " '節に' に 'price.product_id' 不明な列"

なぜそれが起こるのでしょうか?

答えて

2

句は次のように解釈される「から」あなた:だから

from meta as price, (meta as currency join products on (products.id = price.product_id)

それだけmeta as currencyproductsテーブルについて知っているように利用可能なprice.product_idは、句にはありません。

+0

それは意味があります、私は '通貨'で試してみました。ありがとう – ArturoO

関連する問題