- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的很纠结于 SQL 中的计数和分组。
重要的表是 transaction_items
——如果有人以每辆 100 英镑的价格购买 4 辆相同的自行车,那么就会在 transaction_items
中插入四行,每行一行数量(这样做是有原因的,我不需要 quantity
列。
我需要做的是从 transaction_items
获取所有记录,其中 charge_id = '12345'
并按 transaction_items.usersid
对它们进行分组AND transaction_items.item_code
- 最终给我一个组列表,每个组都与一个唯一的用户和一个唯一的产品相关。因此,如果用户的购物车中有多种产品,则他们可以拥有多个组。如果用户 1 购买了 2 x 商品 1 和 4 x 商品 2,则用户 1 将有两组,一组用于商品 1,计数为 2,一组用于商品 2,计数为 4。目前,我似乎只能得到一组。
所以如果我在 transaction_items 中有这个:
| id | charge_id | usersid | item_code |
| 1 | 12345 | 12 | 66 |
| 2 | 12345 | 12 | 66 |
| 3 | 66666 | 11 | 66 |
| 4 | 12345 | 12 | 66 |
| 5 | 12345 | 6 | 22 |
| 6 | 77777 | 10 | 66 |
| 7 | 12345 | 12 | 66 |
| 8 | 44444 | 23 | 66 |
| 9 | 12345 | 6 | 22 |
我想首先按 charge_id AND usersid AND item_code
对数据进行分组,我想取回此数据:
Array
(
// The array should contain arrays for each pair of usersid's and item_codes, that all have the same charge_id
[0] => Array
(
//This is a unique group for usersid and item_code
[charge_id] => '12345'
[usersid] => '12'
[item_code] => '66'
[num_of_records] => '4'
)
[1] => Array
(
//This is a unique group for usersid and item_code
[charge_id] => '12345'
[usersid] => '6'
[item_code] => '22'
[num_of_records] => '2'
)
)
所以我希望得到一个数组:
我现在得到的似乎只是一个产品ID,一个用户ID,而是反射(reflect)所有用户和项目的记录总数,这与我的目标完全相反! (下面的虚拟输出反射(reflect)了上面示例表的使用):
Array
(
[0] => Array
(
[charge_id] => '12345'
[usersid] => '12'
[item_code] => '66'
[num_of_records] => '6'
)
)
$items = $db->query("
SELECT
count(transaction_items.id) as num_of_records,
transaction_items.*
FROM
transaction_items
WHERE
transaction_items.charge_id = :charge
AND
transaction_items.lmsid = :lmsid
GROUP
BY
transaction_items.usersid,
transaction_items.charge_id,
transaction_items");
我错过了什么?
谢谢!
<小时/>这一切都取决于 WHERE 子句。当我删除它时,查询工作正常,但会收集 items 表中的所有记录,而我只想要 charge_id = '12345'
的记录。因此,我认为这个问题的主题严格与 GROUP BY 和 WHERE 的组合使用以及为什么我没有得到我期望的结果有关。
为了简化问题,现在的查询是:
$items = $db->query("SELECT
count(transaction_items.id) as num_of_records,
item_code,
usersid
FROM
transaction_items
WHERE
charge_id = '12345'
GROUP
BY
usersid,
charge_id,
item_code
");
最佳答案
不确定我是否明白 - 这是你的意思吗?
CREATE TABLE transaction_items
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,charge_id INT NOT NULL
,usersid INT NOT NULL
,item_code INT NOT NULL
);
INSERT INTO transaction_items VALUES
(1 ,12345 ,12 ,66),
(2 ,12345 ,12 ,66),
(3 ,66666 ,11 ,66),
(4 ,12345 ,12 ,66),
(5 ,12345 , 6 ,22),
(6 ,77777 ,10 ,66),
(7 ,12345 ,12 ,66),
(8 ,44444 ,23 ,66),
(9 ,12345 , 6 ,22);
SELECT charge_id
, usersid
, item_code
, COUNT(*)
FROM transaction_items
GROUP
BY charge_id
, usersid
, item_code;
+-----------+---------+-----------+----------+
| charge_id | usersid | item_code | COUNT(*) |
+-----------+---------+-----------+----------+
| 12345 | 6 | 22 | 2 |
| 12345 | 12 | 66 | 4 |
| 44444 | 23 | 66 | 1 |
| 66666 | 11 | 66 | 1 |
| 77777 | 10 | 66 | 1 |
+-----------+---------+-----------+----------+
编辑:我仍然很困惑......
SELECT charge_id
, usersid
, item_code
, COUNT(*)
FROM transaction_items
WHERE charge_id = 12345
GROUP
BY charge_id
, usersid
, item_code;
+-----------+---------+-----------+----------+
| charge_id | usersid | item_code | COUNT(*) |
+-----------+---------+-----------+----------+
| 12345 | 6 | 22 | 2 |
| 12345 | 12 | 66 | 4 |
+-----------+---------+-----------+----------+
!?!?!
关于mysql - SQL GROUP BY(多个组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149276/
我想编写一个 linq 表达式,该表达式将返回不包含特定值的 ID。例如,我想返回所有不具有 Value = 30 的不同 ID。 ID, Value 1, 10 1, 20 1, 30 2,
我正在尝试使用 Regexp 匹配 Nmap 命令的输出。可以有两种不同的格式。 第一种格式(当 nmap 可以找到主机名时) Nmap scan report for 2u4n32t-n4 (192
我正在 Visual Studio 2012 上使用 C# 开发一个软件。我使用 MySQL Connector 6.9.1 进行 MySQL 连接。我的软件在我的操作系统(Win8 x64)上运行顺
在 Django 中(使用 django.contrib.auth 时)我可以添加一个 Group到另一个 Group ?即一个Group成为另一个成员(member) Group ? 如果是这样,我
我试图通过使用动态组参数对数据进行分组来循环。 我们可以在循环的 WHERE 条件上使用动态查询,但我不知道是否可以在组条件中使用动态字符串。 以下是用户决定按哪个字段分组,然后根据决定放置其他逻辑的
我有这样的字符串 s = 'MR1|L2-S1x' 模式总是相同的:一个或两个字符,在 [|.+:x-] 中可选地后跟一个数字和一个分隔符。此模式可以重复 6 次。 所以匹配模式很明确。 p = r'
我有一个带有时间戳字段“bar”的表“foo”。如何仅获取查询的最旧时间戳,例如: SELECT foo.bar from foo?我尝试执行以下操作: SELECT MIN(foo.bar) fro
在我的 Django 项目中,我有一个 user_manage 应用程序。 我在 user_manage 应用的 model.py 中创建了一个名为 UserManage 的模型: from djan
所以我有这样的输入: 还有一个模板指令,例如: 看来我只获得了 foo 和 bar 的组。 (为什么?我预计我可能会得到第三组 current-group-key() = '')。
我正在尝试扩展 django.contrib.auth 并遇到将用户添加到组中的情况,这可以通过两种方式完成。我只是想知道为什么会这样,以及其中一种相对于另一种的优势是什么。 最佳答案 他们做完全相同
我使用的是旧的 PHP 脚本,并且此查询有错误。由于我没有使用 mysql 的经验,因此无法修复它。 "SELECT COUNT(p.postid) AS pid, p.*, t.* FROM ".T
我有几行 Objective-C 代码,例如: ABAddressBookRef addressBook; CFErrorRef error = NULL; addressBook = ABAddre
我正在使用 MariaDB IMDB 电影数据集,我试图解决以下问题。电影表包含 id、名称、排名和年份列 A decade is a sequence of 10 consecutive years
让我从数据开始,以便更好地描述我的需求。我有一个名为 SUPERMARKET 的表,其中包含以下字段: Field 1: StoreID Field 2: ProductCategory Field
你好我有这个查询: SELECT DISTINCT a.id, a.runcd, (SELECT SUM(b.CALVAL) FROM GRS b WHERE b.PCode=11000 AND a.
我想在 xquery 中使用 Group By。有人可以告诉我如何在 Marklogic 中使用 Group By 吗? 最佳答案 或者,您可以使用 xdmp:xslt-invoke 调用 XSLT或
因此,当通过 from sequelize 请求组时,如下所示: return models.WorkingCalendar .findAll({
我希望我解释正确。 我有 2 个表,有 第一个表(table1) +------------+------+-------+-------+ | Date | Item | Block |
我的表 MYTABLE 有 2 列:A 和 B 我有以下代码片段: SELECT MYTABLE.A FROM MYTABLE HAVING SUM(MYTABLE.B) > 100
我有一个简单的行分组查询,需要 0.0045 秒。 300.000 行 从表 GROUP BY cid 中选择 cid 当我添加 MAX() 进行查询时,需要 0.65 秒才能返回。 从表 GROUP
我是一名优秀的程序员,十分优秀!