将表与设置的数据连接起来
我正在为一家商店构建底层数据库。有两张桌子,一张放服装,一张放标准尺码表。服装有一种或多种尺码可供选择,尺码表包含每种尺码服装的多种尺寸。
尺码表示例
id size waist chest hips height
0 6 56 76 82 160
0 8 60 80 86 162
0 10 64 84 90 164
0 12 68 88 94 166
1 6 57 75 82 160
1 8 61 79 86 162
1 10 62 83 90 164
1 12 63 87 94 166
服装表示例
garment_id garment_name garment_type size_chart available_sizes
0 Boating Jacket jacket 0 8,10,12
1 Polka Dot Skirt skirt 1 10,12
我想做的是将它们连接起来,这样我就有了
garment_id ... size_chart available_sizes size_chart.id size_chart.size size_chart.(...)
0 ... 0 8,10,12 0 8
0 ... 0 8,10,12 0 10
0 ... 0 8,10,12 0 12
1 ... 1 10,12 1 10
1 ... 1 10,12 1 12
我遇到的问题是如何连接,以便我获得集合中的每个数字(available_size)以获得相应的测量条目... 任何关于我
如何最好地做到这一点的想法/建议/建议将非常有用!
谢谢。
I am building the underlying database for a shop. There are 2 tables, one for garments and one which holds standard size charts. Garments are available in one or more sizes and the size charts contain several measurements for each size garment.
Example size chart table
id size waist chest hips height
0 6 56 76 82 160
0 8 60 80 86 162
0 10 64 84 90 164
0 12 68 88 94 166
1 6 57 75 82 160
1 8 61 79 86 162
1 10 62 83 90 164
1 12 63 87 94 166
Example garments table
garment_id garment_name garment_type size_chart available_sizes
0 Boating Jacket jacket 0 8,10,12
1 Polka Dot Skirt skirt 1 10,12
What I want to do is join them so that I have
garment_id ... size_chart available_sizes size_chart.id size_chart.size size_chart.(...)
0 ... 0 8,10,12 0 8
0 ... 0 8,10,12 0 10
0 ... 0 8,10,12 0 12
1 ... 1 10,12 1 10
1 ... 1 10,12 1 12
The problem I am having is how to join so that I get every number in the set (available_size) to have a corresponding entry for measurements....
Any ideas/suggestions/advice on how I am best to do this would be really useful!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
示例服装表
是万恶之源 - 所以,请跟着我说:
如果我想单独访问它们,我永远不会在单个数据库字段中存储多个信息。从来没有。
更严重的是,您可能希望从
garments
表中删除available_sizes
字段,而是执行类似的操作并填充它。这样可以轻松地将一种尺码标记为已售完,甚至可以在表中添加
qty_in_stock INT
、搜索具有特定尺码的所有相同类型的服装等。Example garments table
Is the root of all evil - so, speak after me:
I will never ever store more than one information in a single db field, if I want to access them seperately. Never.
On a more serious note, you would want to drop the
available_sizes
field from thegarments
table, instead do something likeand populate it. This makes it easy to mark one size as sold out, maybe even add a
qty_in_stock INT
to the table, search for all garments of same type, which are available in a specific size, etc.您可以对所有行使用交叉联接,然后使用子联接选择初始 SELECT 子句中的以在一列中列出大小。
You can use a cross join for all the rows and then a sub-select inside the initial SELECT clause to list the sizes in one column.
一种方法是使用动态 SQL。根据记忆,它会是这样的:
现在,就像 @EugenRieck 所说,这是邪恶的!
A way to do it is using Dynamic SQL. From memory, it would be simething like this:
Now, like @EugenRieck stated, this is evil!