多个表的简单内连接有问题

时间:2013-04-09 05:29:14

标签: mysql sql inner-join

编辑:做了一点改变。添加了表格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           |
 ----------------------------------------------------------------------

问题:由于Supplier_Speedlookup_supply_speed之间没有任何FK关联,因此我无法生成能够获得db_supply列的SQL语句(所有其他表都这样做。)

下面的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 通过lookup_supply_speed 加入db_supplier,然后将其加入其余部分?

SQLFiddle:http://www.sqlfiddle.com/#!2/9635d/3

2 个答案:

答案 0 :(得分:1)

引用除from子句中on表之外的其他表是可以的:

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

(您将b别名为lookup_supplier。)

答案 1 :(得分: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