【发布时间】:2013-06-04 15:11:00
【问题描述】:
我有两个名为 Product 和 ProductImage 的表。
两个表之间存在 1-n 关系。一种产品和多个图像,具体取决于产品。
我想创建一个产品视图,我想从 ProductImage 表中为每个产品随机获取一张图片。
示例数据:http://sqlfiddle.com/#!6/43c69
我想要下面这样的东西。
+-----------+------+-------------+
| ProductId | Name | WebPath |
+-----------+------+-------------+
| 1 | Foo | foowebpath2 |
| 2 | Boo | boowebpath3 |
| 3 | Zoo | zoowebpath1 |
+-----------+------+-------------+
或
+-----------+------+-------------+
| ProductId | Name | WebPath |
+-----------+------+-------------+
| 1 | Foo | foowebpath1 |
| 2 | Boo | boowebpath1 |
| 3 | Zoo | zoowebpath6 |
+-----------+------+-------------+
或
+-----------+------+-------------+
| ProductId | Name | WebPath |
+-----------+------+-------------+
| 1 | Foo | foowebpath4 |
| 2 | Boo | boowebpath2 |
| 3 | Zoo | zoowebpath5 |
+-----------+------+-------------+
等等……
每次都必须不一样。
【问题讨论】:
标签: sql-server tsql join