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')
)
);
答案 0 :(得分:1)
您需要做一个回文连接(我的术语。)首先找到指定项目的供应商ID,然后从items_suppliers
找到相同供应商ID的所有记录。您加入items
至items_suppliers
至items_suppliers
至items
。然后在最后加入suppliers
以获取供应商名称。
获得所需的强制排序顺序有点棘手。我曾经使用过其他地方见过的黑客,但老实说,我不知道它有多常见。我执行SELECT
只获取Item1
的供应商,并在结果中添加一个值为1
的常量列。我做了一秒SELECT
来获取 Item1
以外的所有商品的供应商,并添加常量列2
。 UNION
这两个查询以及该结果只选择了您想要的列,但是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)
一步一步地做:
查询:
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);
}
。