0

Oracle 查询返回的值如下例所示。但我想订购联系人姓名为 admin,jack,robert ,但下面的顺序不是按字母顺序排列的,同时 prodid 应该是相同的组,如下例所示。

甲骨文查询:

SELECT part_id, prodname, shippingaddress, contact 
FROM part p, address a WHERE p.part_id = a.part_id (+) 
AND p.quarter = a.quarter (+) AND 
p.quarter = '2014' AND order by p.prod_id

以上查询输出:

prodid     prodname    shippingaddress    contact
-------------------------------------------------
01         computer    xxxxxx             Jack
01         computer    xxxxxx             admin
01         computer    xxxxxx             robert
03         keybord     xxxxxx             admin
03         keybord     xxxxx              jack
06         cpu         xxxx               robert
06         cpu         xxxx               admin 
06         cpu         xxx                jack 

预期输出:

prodid     prodname    shippingaddress    contact
-------------------------------------------------
01         computer    xxxxxx             admin
01         computer    xxxxxx             jack
01         computer    xxxxxx             robert
03         keybord     xxxxxx             admin
03         keybord     xxxxx              jack
06         cpu         xxxx               admin
06         cpu         xxxx               jack
06         cpu         xxx                robert

这在 SQL 中可能吗?

4

2 回答 2

2

您可以定义多个级别的 ORDER:

SELECT part_id, prodname, shippingaddress, contact 
FROM part p, address a WHERE p.part_id = a.part_id (+) 
AND p.quarter = a.quarter (+) AND 
p.quarter = '2014' AND order by p.prod_id, contact

使用更新的 JOINS 类似:

SELECT part_id, prodname, shippingaddress, contact 
FROM part p
JOIN address a 
   ON p.part_id = a.part_id
   AND p.quarter = a.quarter
WHERE p.quarter = '2014' 
ORDER BY p.prod_id, contact
于 2013-06-12T20:41:35.310 回答
2

尝试

SELECT part_id, prodname, shippingaddress, contact 
FROM part p, address a WHERE p.part_id = a.part_id (+) 
AND p.quarter = a.quarter (+) AND 
p.quarter = '2014' order by p.prod_id, contact

注意:您的查询使用旧的连接语法...尽量避免这种情况并使用更新的连接语法(寻找内连接、外连接等而不是(+)语法)。

于 2013-06-12T20:43:06.353 回答