编辑:做了一点改变。添加表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 |
----------------------------------------------------------------------
问题:我不能生成一个 SQL 语句来获取我的Supplier_Speed
列,因为lookup_supply_speed
它没有任何 FK 关联db_supply
(所有其他表都有)。
下面的 SQL 会给我这个:(我们查询Itm_Name
例如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'
如何修改它以便我可以加入lookup_supplier
vialookup_supply_speed
然后db_supplier
将其加入其余部分?
SQLFiddle:http ://www.sqlfiddle.com/#!2/9635d/3