将表与设置的数据连接起来

发布于 2024-12-29 05:08:50 字数 1330 浏览 3 评论 0原文

我正在为一家商店构建底层数据库。有两张桌子,一张放服装,一张放标准尺码表。服装有一种或多种尺码可供选择,尺码表包含每种尺码服装的多种尺寸。

尺码表示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经大条 2025-01-05 05:08:50

示例服装表

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

是万恶之源 - 所以,请跟着我说:

如果我想单独访问它们,我永远不会在单个数据库字段中存储多个信息。从来没有。

更严重的是,您可能希望从 garments 表中删除 available_sizes 字段,而是执行类似的操作

CREATE TABLE available_sizes (
  id INT PRIMARY KEY AUTO_INCREMENT,
  garment_id INT NOT NULL,
  INDEX(garment_id),
  size_id INT NOT NULL,
  INDEX(size_id)
)

并填充它。这样可以轻松地将一种尺码标记为已售完,甚至可以在表中添加 qty_in_stock INT、搜索具有特定尺码的所有相同类型的服装等。

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

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 the garments table, instead do something like

CREATE TABLE available_sizes (
  id INT PRIMARY KEY AUTO_INCREMENT,
  garment_id INT NOT NULL,
  INDEX(garment_id),
  size_id INT NOT NULL,
  INDEX(size_id)
)

and 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.

怪我入戏太深 2025-01-05 05:08:50

您可以对所有行使用交叉联接,然后使用子联接选择初始 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.

你是我的挚爱i 2025-01-05 05:08:50

一种方法是使用动态 SQL。根据记忆,它会是这样的:

declare @cmd varchar(1000)

declare @id int
select @id = 0

declare @inVariable varchar(20)
select @inVariable = NULL

while(true)
begin
    select @inVariable = availables_sizes from garment where id = @id

    if (@inVariable <> NULL OR @inVariable <> '')
    begin

        if(@id > 0)
        begin
            select @cmd =
            ' UNION select g.garment_id, g.size_chart, g.availables_sizes, c.id, c.size
            from garments g
            inner join chart c on g.size_chart = c.id
                  and c.id = ' + convert(varchar, @id) + ' 
                  and c.size in (' + @invariable + ')'
        end
        else
        begin
            select @cmd =
            'select g.garment_id, g.size_chart, g.availables_sizes, c.id, c.size
            from garments g
            inner join chart c on g.size_chart = c.id
                  and c.id = ' + convert(varchar, @id) + ' 
                  and c.size in (' + @invariable + ')'
        end

        select @id = @id + 1
    end
    else
    begin
        break
    end
end

exec(@cmd)

现在,就像 @EugenRieck 所说,这是邪恶的!

A way to do it is using Dynamic SQL. From memory, it would be simething like this:

declare @cmd varchar(1000)

declare @id int
select @id = 0

declare @inVariable varchar(20)
select @inVariable = NULL

while(true)
begin
    select @inVariable = availables_sizes from garment where id = @id

    if (@inVariable <> NULL OR @inVariable <> '')
    begin

        if(@id > 0)
        begin
            select @cmd =
            ' UNION select g.garment_id, g.size_chart, g.availables_sizes, c.id, c.size
            from garments g
            inner join chart c on g.size_chart = c.id
                  and c.id = ' + convert(varchar, @id) + ' 
                  and c.size in (' + @invariable + ')'
        end
        else
        begin
            select @cmd =
            'select g.garment_id, g.size_chart, g.availables_sizes, c.id, c.size
            from garments g
            inner join chart c on g.size_chart = c.id
                  and c.id = ' + convert(varchar, @id) + ' 
                  and c.size in (' + @invariable + ')'
        end

        select @id = @id + 1
    end
    else
    begin
        break
    end
end

exec(@cmd)

Now, like @EugenRieck stated, this is evil!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文