【发布时间】:2014-07-16 18:18:33
【问题描述】:
我有以下表格
customers(custid, name)
orders(orderid, custid)
itemsordered(itemid, orderid)
items(itemid, description)
我的目标是让每对客户都订购了具有相同描述的商品,检索这两个客户的姓名。消除重复,不要将客户与他们自己配对,并且每对只包含一次。对于每一对,按字母顺序返回该对中的名称。
我知道我需要引用两个 custid 并将它们的描述相互比较,例如:
select nameA
from customers nameA
join orders using (custid)
join itemsordered using (orderid)
join item using (itemid)
where (select nameB
from customers
from customers nameA
join orders using (custid)
join itemsordered using (orderid)
join item using (itemid)
where descriptionA = descriptionB
etc.
但我不确定如何进行。正确答案如下:
Christina|Janine
Christina|Max
Christina|Teddy
Christina|David
Christina|Rachel
Rachel|Teddy
David|Janine
David|Rachel
David|Teddy
Janine|Rachel
Janine|Teddy
Janine|Max
Max|Teddy
我需要一些新的例子。我的大部分猜测都包含如下子查询:
select attribute from table
join anotherTable using (somekey)
where table in (select anotherAttribute
from anotherTable
where....etc.
任何建议或方向将不胜感激。
更新:名称是唯一的。
【问题讨论】:
-
名字是唯一的吗?如果不是,则输出不明确...
-
名称是唯一的。说明已更新。
标签: sql postgresql