如何从客人购买两件东西的桌子上获取数据......
select TA.orderid as ORDERID,custid,name,description,ITEMID from
(
select T3.custid as CUSTID,orderid,name from
(
select orderid,custid from orders
) as T3
inner join
(select custid, name from customers)as T4
on T3.custid=T4.custid
order by name
)as TA
inner join
(
select T1.itemID as ITEMID, OrderID,description from
(
select itemID,OrderID from ItemsOrdered
)as T1
inner join
(
select itemID, description from Items
)as T2
on T1.itemID=T2.itemID
)as RA
on TA.orderid=RA.orderid
我想要达到的目的是获得已经购买了两件物品的顾客名单。
向正确方向推动将非常有帮助
答案 0 :(得分:3)
为了轻推,我会考虑使用聚合函数以及GROUP
和HAVING
SQL限定符之类的东西。例如,像:
SELECT custid FROM custorder GROUP BY custid HAVING COUNT(*) = 2;
假设其名称为custorder
,将仅返回从上面列出的表中购买两次的客户的ID列表。显然,如果这是加入操作的结果,您需要将custorder
替换为JOIN
。
希望这有帮助。
答案 1 :(得分:0)
实际上,@ Feneric,你的贡献可能会使提问者比解决问题更麻烦,因为你提供了一个查询来识别带来超过一个项目的客人,而不是两个项目。
怎么回事:
SELECT custid
FROM custorder
GROUP BY custid
HAVING COUNT(*) = 2