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           |
 ----------------------------------------------------------------------

问题:我不能生成一个 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_suppliervialookup_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 回答