我有两个表,一个是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 |
+-----------+------+-------------+
或等等...
它每次都必须不同。