【发布时间】:2018-05-25 13:41:45
【问题描述】:
我正在使用事件。我想知道如何计算事件总和或使用单个查询 http://sqlfiddle.com/#!9/ad6d1c/1
问题的 DDL:
CREATE TABLE `table1` (
`id` int(11) NOT NULL,
`group_id` int(11) NOT NULL DEFAULT '0',
`in_use` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0->in_use,1->not_in_use',
`auto_assign` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0->Yes,1->No'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `table1`
ADD PRIMARY KEY (`id`);
ALTER TABLE `table1`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
INSERT INTO `table1` (`id`, `group_id`, `in_use`, `auto_assign`) VALUES
(1, 3, 1, 0),(2, 2, 0,1),(3, 1, 1, 1),(4, 3, 1, 0),(5, 3, 0, 0),(6, 3, 0, 1),
(7, 3, 1, 0),(8, 3, 0, 1),(9, 3, 0, 1),(10, 3, 0, 1),(11, 3, 0, 1),(12, 3, 1, 1),
(13, 3, 1, 0),(14, 3, 0, 0),(15, 3, 0, 0),(16, 3, 0, 0),(17, 3, 0, 0),(18, 3, 1, 1),
(19, 3, 0, 0),(20, 3, 0, 0)
预期输出:
| count | in_use | auto_assign | sum | check_count |
|-------|--------|-------------|------|------------ |
| 7 | 0 | 0 | 11 | 5 |
| 5 | 0 | 1 | 07 | 3 |
| 4 | 1 | 0 | 11 | 5 |
| 2 | 1 | 1 | 07 | 3 |
在这里我们可以看到auto_assign=0 的总数为11 count(7+4) 和
auto_assign=1 有 7 计数(5+2) 这个计数应该存储到新列 sum。
check_count 列是sum 列的百分比值。百分比将被预定义。
让我们取 50%,所以计数 11(总和列值)->50% = 5.5 = ROUND(5.5) == 5(整数)。同样的方法计数 7(总和列值)->50% = 3.5 =ROUND(3.5)=3(Integer)
这里5 > 4(auto_assign=0 and in_use=1 ).所以必须将记录插入另一个表(table2)。如果不是,那就不是。
同理,如果 3 >2 也需要往另一个表中插入记录(table2)。如果没有则不需要。
注意:这个逻辑我想在event中实现
这有点复杂,但请建议我在事件中如何做到这一点。
详细说明:
这里的百分比值是5 为auto_assign =0。但auto_assign=0 and in_use=1 有count 是4 其中less than 5,然后必须将记录插入表2。
假设,如果我们得到auto_assign=0 and in_use=1的计数为6,则不需要在table2中插入记录。
同理,
这里的百分比值是3 为auto_assign =1。但auto_assign=1 and in_use=1 有count 是2 其中less than 3 ,然后必须插入记录到表2。
假设,如果我们得到auto_assign=1 and in_use=1的计数为4,则不需要在table2中插入记录。
将查询插入table2:
Insert into table2(cli_group_id,auto_assign,percentage_value,result_value) values(3,0,5,4)
【问题讨论】:
-
Thia 不是一个完整的问题,因为我们看不到您的数据,并且链接可能会随着时间的推移而中断。请在问题中直接包含输入数据。
-
我不知道如何直接包含有问题的数据。
-
什么版本的mySQL? (8.0 具有分析功能,可以轻松完成。)
-
Mysql版本为5.6
标签: mysql