編集:少し変更しました。表を追加しdb_supplier (Supplier_ID, Supply_Speed_ID)
、に変更lookup_supply_speed (Supplier_ID, Supplier_Speed)
lookup_supply_speed (Supplier_Speed_ID, Supplier_Speed)
私のテーブル:
db_supply
-----------------------------------------------
| Supplier_ID | Supply_Type_ID | Itm_ID | Stock |
|-----------------------------------------------|
| 1 | 1 | 33 | 3 |
|-----------------------------------------------|
| 2 | 2 | 28 | 1 |
-----------------------------------------------
db_supplier
----------------------------------------
| Supplier_ID | Supply_Speed_ID | Etc... |
|----------------------------------------|
| 1 | 1 | |
|----------------------------------------|
| 2 | 2 | |
----------------------------------------
lookup_supplier_name
-----------------------------
| Supplier_ID | Supplier_Name |
|-----------------------------|
| 1 | Walter |
|-----------------------------|
| 2 | Jesse |
-----------------------------
lookup_supply_type
-----------------------------------
| Supply_Type_ID | Supply_Type_Name |
|-----------------------------------|
| 1 | Import |
|-----------------------------------|
| 2 | Delivery |
-----------------------------------
lookup_itm
------------------------
| Itm_ID | Itm_Name |
|------------------------|
| 33 | Pickles |
|------------------------|
| 28 | Burger |
------------------------
lookup_supply_speed
------------------------------------
| Supplier_Speed_ID | Supplier_Speed |
|------------------------------------|
| 1 | Fast |
|------------------------------------|
| 2 | Slow |
------------------------------------
A query for 'Burger' will output this table:
----------------------------------------------------------------------
| Supplier_Name | Supply_Type_Name | Itm_Name | Stock | Supplier_Speed |
|----------------------------------------------------------------------|
| Jesse | Delivery | Burger | 1 | Slow |
----------------------------------------------------------------------
問題:にはFK の関連付けがないため(他のすべてのテーブルには関連付けられている)、Supplier_Speed
列を取得する SQL ステートメントを生成できません。lookup_supply_speed
db_supply
以下のSQLは私にこれを与えます: (WE QUERY FOR Itm_Name
eg Burger
)
-----------------------------------------------------
| Supplier_Name | Supply_Type_Name | Itm_Name | Stock |
|-----------------------------------------------------|
| Jesse | Delivery | Burger | 1 |
-----------------------------------------------------
SELECT b.Supplier_Name,
c.Supply_Type_Name,
d.Itm_Name,
a.Stock
FROM db_supply a
INNER JOIN lookup_supplier_name b
ON a.Supplier_ID = b.Supplier_ID
INNER JOIN lookup_supply_type c
ON a.Supply_Type_ID = c.Supply_Type_ID
INNER JOIN lookup_itm d
ON a.Itm_ID = d.Itm_ID
WHERE d.Itm_Name = 'Burger'
これを変更して、 vialookup_supplier
に参加し、残りの部分に参加できるようにするにはどうすればよいですか?lookup_supply_speed
db_supplier
SQLFiddle: http://www.sqlfiddle.com/#!2/9635d/3