0

編集:少し変更しました。表を追加し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_speeddb_supply

以下のSQLは私にこれを与えます: (WE QUERY FOR Itm_Nameeg 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

4

2 に答える 2

1

from内のテーブル以外のテーブルを参照しても問題ありません。on

INNER JOIN lookup_supply_speed ss
    ON ss.Supplier_ID = b.Supplier_ID

b( lookup_supplier にエイリアスします。)

于 2013-04-09T05:31:58.493 に答える
1

これはどう?

SELECT  f.Supplier_Name,
        b.Supplier_ID,
        c.Supply_Type_Name,
        d.Itm_Name,
        a.Stock,
        e.Supplier_Speed
FROM    db_supply a
        INNER JOIN db_supplier b
            ON a.Supplier_ID = b.Supplier_ID
        INNER JOIN lookup_supplier_name f
            ON f.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
        INNER JOIN lookup_supply_speed e
            ON e.Supplier_Speed_ID = b.Supply_Speed_ID
WHERE   d.Itm_Name = 'Burger'

http://www.sqlfiddle.com/#!2/be1a2/3

于 2013-04-09T05:55:01.180 に答える