mysql 全选,按 orderid 分组

发布于 2024-11-28 04:29:46 字数 2362 浏览 1 评论 0原文

我想知道是否有人可以帮助我从表中选择信息,但根据订单 ID 对结果进行分组。我确信这很简单,但我似乎无法让代码工作。

这是我的尝试 - 只显示 1 个结果,而不是 6 个:

  • orderid 55542 的 4 个结果
  • orderid 55543

SQL 的 2 个结果:

SELECT *
FROM #__users_orders
WHERE userid = 22 
GROUP BY orderid 
ORDER BY date DESC

任何帮助将不胜感激:)

编辑:

我想实现这个(或类似的东西)

Array[55542]
(
    [0] => stdClass Object
        (
            [id] => 6
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:30:24
            [code] => 121021
            [title] => 7 STAR CHICKEN A/KING 71198 1.3KG
            [units] => 2
            [ctns] => 
        )

    [1] => stdClass Object
        (
            [id] => 1
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:06:12
            [code] => 302371
            [title] =>  ANCHOVY FILL 730GM
            [units] => 2
            [ctns] => 
        )

    [2] => stdClass Object
        (
            [id] => 6
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:30:24
            [code] => 121021
            [title] => 7 STAR CHICKEN A/KING 71198 1.3KG
            [units] => 2
            [ctns] => 
        )

    [3] => stdClass Object
        (
            [id] => 1
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:06:12
            [code] => 302371
            [title] =>  ANCHOVY FILL 730GM
            [units] => 2
            [ctns] => 
        )

)

Array[55543]
(
    [0] => stdClass Object
        (
            [id] => 6
            [userid] => 66
            [orderid] => 55543
            [date] => 2011-08-05 16:30:24
            [code] => 121021
            [title] => 7 STAR CHICKEN A/KING 71198 1.3KG
            [units] => 2
            [ctns] => 
        )

    [1] => stdClass Object
        (
            [id] => 1
            [userid] => 66
            [orderid] => 55543
            [date] => 2011-08-05 16:06:12
            [code] => 302371
            [title] =>  ANCHOVY FILL 730GM
            [units] => 2
            [ctns] => 
        )

)

I was wondering if anyone could help me with selecting information from my table, but grouping the results depending on the order id. I'm sure this is quite simple, but I can't seem to get the code working.

Here's my attempt - which is only showing 1 result, instead of 6:

  • 4 results with orderid 55542
  • 2 results with orderid 55543

SQL:

SELECT *
FROM #__users_orders
WHERE userid = 22 
GROUP BY orderid 
ORDER BY date DESC

Any help would be appreciated :)

EDIT:

I'd like to acheive this (or something similar)

Array[55542]
(
    [0] => stdClass Object
        (
            [id] => 6
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:30:24
            [code] => 121021
            [title] => 7 STAR CHICKEN A/KING 71198 1.3KG
            [units] => 2
            [ctns] => 
        )

    [1] => stdClass Object
        (
            [id] => 1
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:06:12
            [code] => 302371
            [title] =>  ANCHOVY FILL 730GM
            [units] => 2
            [ctns] => 
        )

    [2] => stdClass Object
        (
            [id] => 6
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:30:24
            [code] => 121021
            [title] => 7 STAR CHICKEN A/KING 71198 1.3KG
            [units] => 2
            [ctns] => 
        )

    [3] => stdClass Object
        (
            [id] => 1
            [userid] => 66
            [orderid] => 55542
            [date] => 2011-08-05 16:06:12
            [code] => 302371
            [title] =>  ANCHOVY FILL 730GM
            [units] => 2
            [ctns] => 
        )

)

Array[55543]
(
    [0] => stdClass Object
        (
            [id] => 6
            [userid] => 66
            [orderid] => 55543
            [date] => 2011-08-05 16:30:24
            [code] => 121021
            [title] => 7 STAR CHICKEN A/KING 71198 1.3KG
            [units] => 2
            [ctns] => 
        )

    [1] => stdClass Object
        (
            [id] => 1
            [userid] => 66
            [orderid] => 55543
            [date] => 2011-08-05 16:06:12
            [code] => 302371
            [title] =>  ANCHOVY FILL 730GM
            [units] => 2
            [ctns] => 
        )

)

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

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

发布评论

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

评论(4

勿忘初心 2024-12-05 04:29:46
SELECT *
FROM #__users_orders
WHERE userid = 22 
ORDER BY orderid DESC

只需像这样选择您的项目,然后通过迭代结果并为出现的每个新 orderid 创建一个新数组,在前端创建对象/数组层次结构。

SELECT *
FROM #__users_orders
WHERE userid = 22 
ORDER BY orderid DESC

Just select your items like this and create your object/array hierarchy in the frontend by iterating over the results and creating a new array for every new orderid that comes by.

心不设防 2024-12-05 04:29:46
SELECT orderid, COUNT(*)
FROM #__users_orders
WHERE userid = 22 
GROUP BY orderid 
ORDER BY date DESC

通常,您必须使用聚合(例如 COUNT、SUM)和 GROUP BY 进行匹配。因此,SELECT 中但 COUNT 或 SUM 中不存在的列属于 GROUP BY

只有 MySQL 允许您遵循此规则。其他数据库引擎会给出错误。

SELECT orderid, COUNT(*)
FROM #__users_orders
WHERE userid = 22 
GROUP BY orderid 
ORDER BY date DESC

Normally, you have to use an aggregate (eg COUNT, SUM) and GROUP BY matched. So that columns in the SELECT but not in the COUNT or SUM are in the GROUP BY

Only MySQL allows you to not follow this rule. Other DB engines would give an error.

大海や 2024-12-05 04:29:46

查询似乎没问题,也许您提取结果的方式错误

SELECT * FROM 表 GROUP BY 字段 1;

应该返回相同的行数

从表 GROUP BY 字段 1 中选择字段 1、字段 2;

但数量不同

从表中选择*;

the query seems ok, maybe your extracting the results the wrong way

SELECT * FROM table GROUP BY field1;

should return same number of rows than

SELECT field1, field2 FROM table GROUP BY field1;

but different number than

SELECT * FROM table;

四叶草在未来唯美盛开 2024-12-05 04:29:46

您不应在此查询中选择“*”。

当您按“某些列”分组时。您只能选择“某些列”或“some_aggregate_function”(其他列)。

例如,如果您想获取每个订单 ID 的总订单大小和最新日期,您可以执行以下操作 -

SELECT orderid, sum(ordersize), max(date) FROM #__users_orders WHERE userid = 22 GROUP BY orderid ORDER BY max(date) DESC

You should not select "*" in this query.

When you group by "some columns". You can only select "some columns" or some_aggregate_function(other columns).

e.g. If you want to get the aggregate ordersize and latest date for each order id, you would do something like -

SELECT orderid, sum(ordersize), max(date) FROM #__users_orders WHERE userid = 22 GROUP BY orderid ORDER BY max(date) DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文