【发布时间】:2013-01-22 14:50:11
【问题描述】:
我在Oracle数据库中有两张表(10g express)
- 产品
- product_image
一个产品可以有多个图像。因此,product 和 product_image 之间存在一对多关系,product_image 表有一个外键引用 product 表的主键。
我需要获取一个产品列表,在要检索的结果集的每一行中只有一个图像名称,而不管product_image中的图像如何表(即使某些产品没有图片)。
要从product_image 表中检索的图像名称一般是在对每个产品的每组图像进行排序后,product_image 表中的第一个图像名称按升序排列。类似于以下内容。
prod_id prod_name prod_image
1 aaa aaa.jpg //The first image name in the product_image table after sorting images for prod_id in ascending order.
2 bbb bbb.jpg //Similar to the first case.
3 ccc - //No image(s) found in the product_image table
4 ddd - //Similar to the previous case.
这两个表的一般连接语句类似于以下内容。
SELECT p.prod_id, p.prod_name, pi.prod_image
FROM product p
INNER JOIN product_image pi
ON p.prod_id=pi.prod_id;
这可以使用单个 SQL 语句吗?
【问题讨论】:
标签: oracle oracle10g greatest-n-per-group