假设我有这些表,从中我需要在浏览器中显示的搜索结果:将多个结果组合为单个字段的子查询?
Table: Containers
id | name
1 Big Box
2 Grocery Bag
3 Envelope
4 Zip Lock
Table: Sale
id | date | containerid
1 20100101 1
2 20100102 2
3 20091201 3
4 20091115 4
Table: Items
id | name | saleid
1 Barbie Doll 1
2 Coin 3
3 Pop-Top 4
4 Barbie Doll 2
5 Coin 4
我需要输出,看起来像这样:
itemid itemname saleids saledates containerids containertypes
1 Barbie Doll 1,2 20100101,20100102 1,2 Big Box, Grocery Bag
2 Coin 3,4 20091201,20091115 3,4 Envelope, Zip Lock
3 Pop-Top 4 20091115 4 Zip Lock
的重要组成部分,是每个项目类型只在屏幕上返回一个记录/行。过去,我通过返回多行相同的项目并使用脚本语言来限制输出来完成此任务。但是,这使得ui过于复杂和糟糕。所以,我希望我可以让数据库只显示与显示行数一样多的记录。
这个例子可能有点极端,因为从物品到达货柜(通过销售表)需要2个连接。
我会很高兴的只是输出这样一个例子查询:
itemid itemname saleids saledates
1 Barbie Doll 1,2 20100101,20100102
2 Coin 3,4 20091201,20091115
3 Pop-Top 4 20091115
我只能在一个子查询返回一个结果,所以我不知道如何做到这一点。
您正在使用哪种RDBMS? – 2010-06-16 18:27:01