SQL使用FOREIGN KEYS

时间:2016-03-30 16:24:35

标签: mysql sql join foreign-keys relational-database

suppliers表:

PK_ID  supplier  supplier_code
  1       x           abc
  2       y           def

items表:

PK_ID   item_name   
 1        Name1           
 2        Name2
带有 FOREIGN KEYS的

items_suppliers

FK_ID_items  FK_ID_suppliers           
    1              1
    2              1  

我想

suppliers表中选择一个项目并查找具有相同供应商的其他项目。 它应该返回" Name2 "项目然后。

[编辑]

选择ITEM1及其全部 suppliers,然后找到匹配item 的每个supplier 每个匹配的item下一个supplier

正确的答案(对@DarwinvonCorax的信用 - 与@ThorstenKettner宝贵的帮助):

SELECT i2.item_name, s.supplier, s.supplier_code
FROM items i1
JOIN items_suppliers is1
ON i1.PK_ID = is1.FK_ID_items
JOIN items_suppliers is2
ON is1.FK_ID_suppliers = is2.FK_ID_suppliers
JOIN items i2
ON is2.FK_ID_items = i2.PK_ID
JOIN suppliers s
ON is1.FK_ID_suppliers = s.PK_ID
WHERE i1.item_name = 'Name1'
ORDER BY
CASE WHEN i2.item_name = 'Name1'
     THEN 1
     ELSE 2
  END,
i2.item_name, s.supplier;

@ThorstenKettner提出的解决方案也是正确的

select item_name
 from items
where pk_id in
(
 select fk_id_items
   from items_suppliers
  where fk_id_suppliers in
  (
    select fk_id_suppliers  
    from items_suppliers
    where fk_id_items = 
      (
       select pk_id from items where item_name = 'Name1')
      )
 );

2 个答案:

答案 0 :(得分:1)

您需要做一个回文连接(我的术语。)首先找到指定项目的供应商ID,然后从items_suppliers找到相同供应商ID的所有记录。您加入itemsitems_suppliersitems_suppliersitems。然后在最后加入suppliers以获取供应商名称。

获得所需的强制排序顺序有点棘手。我曾经使用过其他地方见过的黑客,但老实说,我不知道它有多常见。我执行SELECT只获取Item1的供应商,并在结果中添加一个值为1的常量列。我做了一秒SELECT来获取 Item1以外的所有商品的供应商,并添加常量列2UNION这两个查询以及该结果只选择了您想要的列,但是ORDER BY常量,它将对值为1的所有行进行排序(Item1的供应商)第一

SELECT item_name, supplier, supplier_code
  FROM
    (SELECT '1' AS orderer, i1.item_name AS item_name, s.supplier AS supplier, s.supplier_code AS supplier_code
      FROM items i1
      JOIN items_suppliers is1
        ON i1.PK_ID = is1.FK_ID_items
      JOIN suppliers s
        ON is1.FK_ID_suppliers = s.PK_ID
      WHERE i1.item_name = 'Name1'
    UNION ALL
    SELECT '2', i2.item_name, s.supplier, s.supplier_code
      FROM items i1
      JOIN items_suppliers is1
        ON i1.PK_ID = is1.FK_ID_items
      JOIN items_suppliers is2
        ON is1.FK_ID_suppliers = is2.FK_ID_suppliers
      JOIN items i2
        ON is2.FK_ID_items = i2.PK_ID
      JOIN suppliers s
        ON is1.FK_ID_suppliers = s.PK_ID
      WHERE i1.item_name = 'Name1'
        AND i2.PK_ID <> i1.PK_ID
    ) r
  ORDER BY orderer, item_name, supplier;

根据Thorsten Kettner的评论,我发现我的SQL只是有点生疏而且已经过时了;他的建议看起来像这样:

SELECT i2.item_name, s.supplier, s.supplier_code
  FROM items i1
  JOIN items_suppliers is1
    ON i1.PK_ID = is1.FK_ID_items
  JOIN items_suppliers is2
    ON is1.FK_ID_suppliers = is2.FK_ID_suppliers
  JOIN items i2
    ON is2.FK_ID_items = i2.PK_ID
  JOIN suppliers s
    ON is1.FK_ID_suppliers = s.PK_ID
  WHERE i1.item_name = 'Name1'
  ORDER BY
    CASE WHEN i2.item_name = 'Name1'
         THEN 1
         ELSE 2
      END,
    i2.item_name, s.supplier;

答案 1 :(得分:0)

一步一步地做:

  1. 找到商品&#39; Name1&#39;。
  2. 的商品ID
  3. 查找该商品ID的所有供应商。
  4. 查找这些供应商的所有商品ID。
  5. 找到找到的ID的项目名称。
  6. 查询:

    select item_name
    from items
    where pk_id in
    (
      select fk_id_items
      from items_suppliers
      where fk_id_suppliers in
      (
        select fk_id_suppliers  
        from items_suppliers
        where fk_id_items = (select pk_id from items where item_name = 'Name1')
      )
    );
    

    如果要从列表中排除and item_name <> 'Name1',可以添加int quantity = 0;; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_trial_order); } public void increment(View view){ quantity = quantity + 1; display(quantity); } public void decrement(View view){ quantity = quantity - 1; display(quantity); }