- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Mysql 表“Event”,它有一个虚拟列“campaignId”。我试图在此列上创建索引但没有成功。
当我尝试创建索引时:
ALTER TABLE `botbit`.`Event`
ADD INDEX `IndexName` (`campaignId` ASC);
我收到这个错误
Error Code: 1366. Incorrect integer value: 'null' for column 'campaignId' at row 1
campaignId 列是虚拟的,其定义如下:
ALTER TABLE `botbit`.`Event`
ADD COLUMN `campaignId` INT(11) GENERATED ALWAYS AS (case when (json_unquote(json_extract(`customProps`,'$.campaignId')) IS NOT NULL) then json_unquote(json_extract(`customProps`,'$.campaignId')) else -4000 end);
如果 json 属性 campaignId 不存在,则 campaignId 的值设置为 -4000(以避免空值)。
我还测试了该列中是否存在空值,但没有:
select * from Event where campaignId IS NULL LIMIT 1;
0 row(s) returned
如果我在该列中没有任何空值,我不明白为什么 mysql 会告诉我“不正确的值:列 campaignId 为空”。
我在其他虚拟列上有其他索引并且工作正常,即使存在 NULL 值也是如此。所以,我认为应该有一些我无法弄清楚的数据问题。
编辑:寻找字符串“空”值我得到了这个结果
SELECT id,campaignId FROM tbl
WHERE json_unquote(json_extract(`customProps`,'$.campaignId'))
= 'null';
| 21096314 | 0 |
| 21096315 | 0 |
| 21096316 | 0 |
| 21096317 | 0 |
| 21096318 | 0 |
| 21096319 | 0 |
| 21096320 | 0 |
| 21096321 | 0 |
| 21096322 | 0 |
| 21096323 | 0 |
| 21096324 | 0 |
编辑 2:创建表
CREATE TABLE `Event` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` smallint(5) unsigned NOT NULL,
`subType` smallint(5) unsigned DEFAULT NULL,
`storeId` mediumint(9) NOT NULL,
`userId` int(11) DEFAULT NULL,
`source` smallint(5) unsigned NOT NULL DEFAULT '0',
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`customProps` json DEFAULT NULL,
`timestamp_i` int(11) DEFAULT NULL,
`mac` varchar(17) COLLATE utf8mb4_bin GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.mac'))) VIRTUAL,
`deviceId` int(11) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.deviceId'))) VIRTUAL,
`poc` int(11) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.poc'))) VIRTUAL,
`registeredThrough` varchar(45) COLLATE utf8mb4_bin GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.registeredThrough'))) VIRTUAL,
`pointOfContactId` int(11) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.pointOfContactId'))) VIRTUAL,
`ticket` decimal(10,0) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.ticket'))) VIRTUAL,
`promoCodeId` int(11) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.promoCodeId'))) VIRTUAL,
`date` date GENERATED ALWAYS AS (cast(`timestamp` as date)) VIRTUAL,
`npsScore` int(11) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.score'))) VIRTUAL,
`npsComment` varchar(2000) COLLATE utf8mb4_bin GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.comment'))) VIRTUAL,
`campaignName` varchar(2000) COLLATE utf8mb4_bin GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.campaignName'))) VIRTUAL,
`productId` int(11) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.productId'))) VIRTUAL,
`reservationId` int(11) GENERATED ALWAYS AS (json_unquote(json_extract(`customProps`,'$.id'))) VIRTUAL,
`campaignId` int(11) GENERATED ALWAYS AS ((case when (json_unquote(json_extract(`customProps`,'$.campaignId')) is not null) then json_unquote(json_extract(`customProps`,'$.campaignId')) else -(4000) end)) VIRTUAL,
`isCustomCampaign` tinyint(1) GENERATED ALWAYS AS ((case when (json_unquote(json_extract(`customProps`,'$.isCustomCampaign')) = 'true') then 1 when (json_unquote(json_extract(`customProps`,'$.isCustomCampaign')) = 'false') then 0 else 0 end)) VIRTUAL,
PRIMARY KEY (`id`),
UNIQUE KEY `Event_id_uindex` (`id`),
KEY `Event_userId_index` (`userId`),
KEY `Event_subType_storeId_index` (`subType`,`storeId`),
KEY `Event_timetamp_index` (`timestamp`),
KEY `Event_subtype_storeId_userId_timestamp_index` (`subType`,`userId`,`storeId`,`timestamp`),
KEY `Event_storeId_type` (`storeId`,`type`),
KEY `Event_mac_index` (`mac`),
KEY `Event_deviceId_index` (`deviceId`)
) ENGINE=InnoDB AUTO_INCREMENT=30693655 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
编辑 3:记录插入示例
INSERT INTO `Event` (`type`, `subType`, `storeId`, `userId`, `source`, `timestamp`, `customProps`) VALUES ('1', '16', '3', '1', '2', '2018-06-04 15:41:56', '{ \"campaignId\": 100, \"isCustomCampaign\": false }');
谢谢
最佳答案
边注:
ADD COLUMN `campaignId` INT(11)
GENERATED ALWAYS AS (
case when (json_unquote(json_extract(`customProps`,'$.campaignId')) IS NOT NULL)
then json_unquote(json_extract(`customProps`,'$.campaignId'))
else -4000 end);
更简单:
ADD COLUMN `campaignId` INT(11)
GENERATED ALWAYS AS (
COALESCE(json_unquote(json_extract(`customProps`,'$.campaignId')),
-4000)
至于问题,看你得到什么
SELECT * FROM tbl
WHERE json_unquote(json_extract(`customProps`,'$.campaignId'))
= 'null';
我认为 JSON 中某处有 4 个字母的字符串 "null"
。
(评论后)
mysql> SET @j := '{ \"campaignId\": 100, \"isCustomCampaign\": false }';
SELECT json_unquote(json_extract(@j, '$.campaignId')) AS the_value,
json_unquote(json_extract(@j, '$.campaignId')) = 'null' AS string_cmp,
json_unquote(json_extract(@j, '$.campaignId')) IS NULL AS null_cmp,
case when (json_unquote(json_extract(@j, '$.campaignId')) IS NOT NULL)
then json_unquote(json_extract(@j, '$.campaignId'))
else -4000 end AS the_case;
+-----------+------------+----------+----------+
| the_value | string_cmp | null_cmp | the_case |
+-----------+------------+----------+----------+
| 100 | 0 | 0 | 100 |
+-----------+------------+----------+----------+
SET @j := '{ \"isCustomCampaign\": false }';
(then same query)
+-----------+------------+----------+----------+
| the_value | string_cmp | null_cmp | the_case |
+-----------+------------+----------+----------+
| NULL | NULL | 1 | -4000 |
+-----------+------------+----------+----------+
这是否为您提供了更多线索?也许它还为您提供了一种在不涉及大表的情况下试验 JSON 的方法。
关于mysql - 在虚拟列上创建 mysql 索引时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54714710/
有WHERE 1=1有什么作用如果您在脚本(伪代码)中编写此请求: sql = "SELECT f1,f2,f3 FROM t WHERE 1=1" ++ restOfTheClause
这个问题已经有答案了: R: Convert delimited string into variables (3 个回答) 已关闭 5 年前。 我有一个包含电影数据的表,在最后一列中,它包含电影所属
假设我有一个基类: struct A{ virtual void foo() = 0; }; 然后假设我有一个这样的派生类: struct B : public virtual A{ voi
我有一个小问题,我的 << 运算符没有被正确调用。 这是我的: class SomeInterface { friend std::ostream& operator<<(std::ostrea
首先,我来自 Java 社区,并且仍然是 C++ 的学习者。 请看下面的类 第二张图片显示了类“GameObject”的子类。它还有一个 Display() 方法。 GameObject类有5个子类,
我这里遇到了一些问题。我试图让我的代码像 java 中的接口(interface)一样工作。这个类被其他 2 个继承,因为它们导致了一些问题。而且我还想知道我是否做对了,以及改进我的代码的方法。我是新
在 C++ 中,我有一个基类 A,一个子类 B。两者都有虚方法 Visit。我想在 B 中重新定义“访问”,但 B 需要访问每个 A(以及所有子类)的“访问”功能。 我有类似的东西,但它告诉我 B 无
我有一个抽象类,它是类层次结构的根。该根类有一个带有一些简单实现的方法,似乎没有必要随时随地更改该实现。 使该方法成为非虚方法很好,但是某些子类可能会意外地重新实现它。在这种情况下,虚拟 final方
在 MSDN 上,我发现在抽象方法声明中使用“virtual”修饰符是错误的。我的一位同事应该是非常有经验的开发人员,但他在他的代码中使用了这个: public abstract class Busi
C++ 虚函数表是仅用于确定调用虚函数时应该执行哪一段代码,还是在运行时有其他用途? 在维基百科上,它列出了“动态调度”作为一个原因,但没有深入了解 C++ 的更多细节...... 最佳答案 一些实现
页面大小是否恒定?更具体地说,getconf PAGE_SIZE 给出 4096,这很公平。但这可以通过程序的运行时间改变吗?或者它在整个操作系统进程生成过程中是否保持不变。 IE。 , 进程是否可能
析构函数(当然还有构造函数)和其他成员函数之间的区别在于,如果常规成员函数在派生类中具有主体,则仅执行派生类中的版本。而在析构函数的情况下,派生版本和基类版本都会被执行? 很高兴知道在析构函数(可能是
如果一个函数被定义为虚函数并且与纯虚函数相同,这究竟意味着什么? 最佳答案 来自 Wikipedia's Virtual function... In object-oriented programm
我有一个在 Jetty 下运行的应用程序,我希望该应用程序返回自引用绝对 URL(生成 RSS 提要时,因此客户端必须能够在没有“当前 URL”上下文的情况下工作)。 问题是我事先不知道应用程序将部署
如何在两个virtualtreeview之间复制以复制所有列,而不仅仅是第一列? 复制前: 复制后: 最佳答案 树控件不保存任何数据。它不包含要显示的列数据,因此无法复制它。而是,当树控件想要显示任何
我已将 ShowHint 设置为 true 并将 HintMode 设置为 hmToolTip,但是当我将光标悬停在控件上时,我的 OnGetHint() 事件处理程序甚至没有断点。 知道我做错了什么
我的 friend 正在 Delphi 中使用 VirtualTreeView 工作,并且遇到了下一个问题:他有两列,第一列的每一行都有数据和子项。是否可以不更改第一列宽度来设置最大子列宽度? 图例:
我在我的 Virtual TreeView Component 中使用 TVirtualStringTree ( Delphi project 的一部分)我想创建一个 View ,其中 2 列可以有可
我想遍历 VirtualTreeView 的所有根并将其删除。 我不想清除它。 我收到此代码的访问冲突: var Node : PVirtualNode; begin if VirtualStri
我有一个可以输出表单的 PHP 文件。我想在服务器端调用这个 PHP 文件(当前使用“include”),填写并提交。 这样更好,因此我不必干预实际的 PHP 表单,只需处理表示层,以便数据可以被它自
我是一名优秀的程序员,十分优秀!