MySQL,如何重复同一行x次

发布于 2024-11-07 12:46:58 字数 983 浏览 0 评论 0原文

我有一个输出地址订单数据的查询:

SELECT ordernumber
  , article_description
  , article_size_description
  , concat(NumberPerBox,' pieces') as contents
  , NumberOrdered
FROM customerorder
WHERE customerorder.id = 1;

我希望将上面的行输出为 NumberOrders(例如 50,000)除以 NumberPerBox 例如 2,000 = 25 次。

是否有一个 SQL 查询可以做到这一点,我不反对使用临时表来连接,如果需要的话。

我检查了之前的问题,但是最近的一个:
是可能在mysql中重复相同的结果
只给出了固定行数的答案,我需要它根据 (NumberOrdered div NumberPerBox) 的值是动态的。

我想要的结果是:

Boxnr   Ordernr        as_description   contents   NumberOrdered
------+--------------+----------------+-----------+---------------
  1   | CORDO1245    | Carrying bags  | 2,000 pcs | 50,000
  2   | CORDO1245    | Carrying bags  | 2,000 pcs | 50,000
....
  25  | CORDO1245    | Carrying bags  | 2,000 pcs | 50,000

I have a query that outputs address order data:

SELECT ordernumber
  , article_description
  , article_size_description
  , concat(NumberPerBox,' pieces') as contents
  , NumberOrdered
FROM customerorder
WHERE customerorder.id = 1;

I would like the above line to be outputted NumberOrders (e.g. 50,000) divided by NumberPerBox e.g. 2,000 = 25 times.

Is there a SQL query that can do this, I'm not against using temporary tables to join against if that's what it takes.

I checked out the previous questions, however the nearest one:
is to be posible in mysql repeat the same result
Only gave answers that give a fixed number of rows, and I need it to be dynamic depending on the value of (NumberOrdered div NumberPerBox).

The result I want is:

Boxnr   Ordernr        as_description   contents   NumberOrdered
------+--------------+----------------+-----------+---------------
  1   | CORDO1245    | Carrying bags  | 2,000 pcs | 50,000
  2   | CORDO1245    | Carrying bags  | 2,000 pcs | 50,000
....
  25  | CORDO1245    | Carrying bags  | 2,000 pcs | 50,000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

噩梦成真你也成魔 2024-11-14 12:46:58

首先声明一下,我对SQL Server比较熟悉,所以我的回答有点偏向。
其次,我没有测试我的代码示例,它可能应该用作开始的参考点。

在我看来,这种情况是数字表的主要候选者。简单地说,它是一个表(通常称为“数字”),只不过是 1 到 n 之间的整数的单个 PK 列。一旦您使用了 Numbers 表并了解了它的使用方式,您将开始发现它的许多用途 - 例如查询时间间隔、字符串分割等。

也就是说,这是我对您的问题的未经测试的回答

SELECT
   IV.number as Boxnr
  ,ordernumber  
  ,article_description
  ,article_size_description
  ,concat(NumberPerBox,' pieces') as contents
  ,NumberOrdered
FROM
  customerorder
  INNER JOIN (
    SELECT
       Numbers.number
      ,customerorder.ordernumber
      ,customerorder.NumberPerBox
    FROM
      Numbers
      INNER JOIN customerorder
        ON Numbers.number BETWEEN 1 AND customerorder.NumberOrdered / customerorder.NumberPerBox
    WHERE
      customerorder.id = 1
    ) AS IV
    ON customerorder.ordernumber = IV.ordernumber

:我说,我的大部分经验都是在SQL Server方面。我参考http://www.sqlservercentral.com/articles/Advanced+Querying/2547/ (需要注册)。但是,当我搜索“SQL 数字表”时,似乎有相当多的可用资源。

First, let me say that I am more familiar with SQL Server so my answer has a bit of a bias.
Second, I did not test my code sample and it should probably be used as a reference point to start from.

It would appear to me that this situation is a prime candidate for a numbers table. Simply put, it is a table (usually called "Numbers") that is nothing more than a single PK column of integers from 1 to n. Once you've used a Numbers table and aware of how it's used, you'll start finding many uses for it - such as querying for time intervals, string splitting, etc.

That said, here is my untested response to your question:

SELECT
   IV.number as Boxnr
  ,ordernumber  
  ,article_description
  ,article_size_description
  ,concat(NumberPerBox,' pieces') as contents
  ,NumberOrdered
FROM
  customerorder
  INNER JOIN (
    SELECT
       Numbers.number
      ,customerorder.ordernumber
      ,customerorder.NumberPerBox
    FROM
      Numbers
      INNER JOIN customerorder
        ON Numbers.number BETWEEN 1 AND customerorder.NumberOrdered / customerorder.NumberPerBox
    WHERE
      customerorder.id = 1
    ) AS IV
    ON customerorder.ordernumber = IV.ordernumber

As I said, most of my experience is in SQL Server. I reference http://www.sqlservercentral.com/articles/Advanced+Querying/2547/ (registration required). However, there appears to be quite a few resources available when I search for "SQL numbers table".

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