Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-选择时使用临时列计算行总数_Mysql - Fatal编程技术网

MySQL-选择时使用临时列计算行总数

MySQL-选择时使用临时列计算行总数,mysql,Mysql,我有一个表(4列),如 我创建了一个简单的数据 INSERT INTO `myTable` (`name`, `refer_id`, `groupType`) VALUES ('a', 1, 1), ('b', 2, 1), ('c', 3, 1), ('d', 4, 2), ('e', 4, 2), ('f', 4, 2), ('g', 7, 2), ('h', 7, 2), ('i', 5, 3), ('k', 5, 3), ('l', 6, 3); INSERT INTO `myT

我有一个表(4列),如

我创建了一个简单的数据

INSERT INTO `myTable` (`name`, `refer_id`, `groupType`) VALUES
('a', 1, 1),
('b', 2, 1),
('c', 3, 1),

('d', 4, 2),
('e', 4, 2),
('f', 4, 2),

('g', 7, 2),
('h', 7, 2),

('i', 5, 3),
('k', 5, 3),
('l', 6, 3);
INSERT INTO `myTable` (`name`, `refer_id`, `groupType`) VALUES
('a', 1, 1),
('b', 2, 1),
('c', 3, 1),

('d', 4, 2),
('e', 4, 2),
('f', 4, 2),

('g', 7, 2),
('h', 7, 2),

('i', 5, 3),
('k', 5, 3),
('l', 6, 3);
我有3个
groupType
。而
groupType
1和3相同(不能分组) 和
groupType
2(组是否具有相同的
参考id
)。 看起来像

id  groupType   group_field total
1           1          1    1
2           1          2    1
3           1          3    1
4           2       group_4 3
7           2       group_7 2
9           3          9    1
10          3         10    1
11          3         11    1
我正在使用query来实现这一点(我认为它看起来不错)

我想通过下面的方法获得上述查询的总行数(
8行
),但这是不正确的。怎么做?谢谢(我不想要所有人)

也许是什么

id  groupType   group_field total defaultValue totalCount
1           1          1    1     1            8
2           1          2    1     1            8
3           1          3    1     1            8
4           2       group_4 3     1            8
7           2       group_7 2     1            8
9           3          9    1     1            8
10          3         10    1     1            8
11          3         11    1     1            8
试试这个

id | groupType | group | U字段| total | cnt -: | --------: | :---------- | ----: | --: 1 | 1 | 1 | 1 | 8 2 | 1 | 2 | 1 | 8 3 | 1 | 3 | 1 | 8 4 | 2 |组4 | 3 | 8 7 | 2 |组7 | 2 | 8 9 | 3 | 9 | 1 | 8 10 | 3 | 10 | 1 | 8 11 | 3 | 11 | 1 | 8
dbfiddle

预期结果是什么?@Simonare我想知道第一次查询中的行总数,但我试图得到
8行(行总数)?结果正好有8行。我不明白你需要什么。预期的输出是什么?很抱歉,我尝试获取数字(
8
)来进行分页。请看我的编辑我试图在
sqlfiddle
上运行它,但它
错误
靠近
()
?Mybe是否取决于MySQL版本?MySQL 8支持窗口函数。
select id,
groupType,
IF(groupType != 2, @gf:=id, @gf:=CONCAT('group_',refer_id)) as group_field,
count(*) as total,

CONCAT_WS('', 1) AS defaultValue,
SUM(defaultValue) as totalCount

from myTable
group by group_field
order by id asc
id  groupType   group_field total defaultValue totalCount
1           1          1    1     1            8
2           1          2    1     1            8
3           1          3    1     1            8
4           2       group_4 3     1            8
7           2       group_7 2     1            8
9           3          9    1     1            8
10          3         10    1     1            8
11          3         11    1     1            8
CREATE TABLE IF NOT EXISTS `myTable` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `refer_id` int(12) NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `groupType` int(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
INSERT INTO `myTable` (`name`, `refer_id`, `groupType`) VALUES
('a', 1, 1),
('b', 2, 1),
('c', 3, 1),

('d', 4, 2),
('e', 4, 2),
('f', 4, 2),

('g', 7, 2),
('h', 7, 2),

('i', 5, 3),
('k', 5, 3),
('l', 6, 3);
select min(id) id,
groupType,
IF(groupType != 2, @gf:=id, @gf:=CONCAT('group_',refer_id)) as group_field,
count(*)  as total,
count(*) over () as cnt
from myTable
group by groupType, group_field 
order by id asc
id | groupType | group_field | total | cnt -: | --------: | :---------- | ----: | --: 1 | 1 | 1 | 1 | 8 2 | 1 | 2 | 1 | 8 3 | 1 | 3 | 1 | 8 4 | 2 | group_4 | 3 | 8 7 | 2 | group_7 | 2 | 8 9 | 3 | 9 | 1 | 8 10 | 3 | 10 | 1 | 8 11 | 3 | 11 | 1 | 8
select min(id) id,
groupType,
IF(groupType != 2, @gf:=id, @gf:=CONCAT('group_',refer_id)) as group_field,
count(*)  as total,
(select count(distinct IF(groupType != 2, @gf:=id, @gf:=CONCAT('group_',refer_id))) from myTable) cnt
from myTable
group by groupType, group_field 
order by id asc
id | groupType | group_field | total | cnt -: | --------: | :---------- | ----: | --: 1 | 1 | 1 | 1 | 8 2 | 1 | 2 | 1 | 8 3 | 1 | 3 | 1 | 8 4 | 2 | group_4 | 3 | 8 7 | 2 | group_7 | 2 | 8 9 | 3 | 9 | 1 | 8 10 | 3 | 10 | 1 | 8 11 | 3 | 11 | 1 | 8