我正在尝试创建一个表,该表包含引用JSON的索引虚拟列。
我创建了一个包含名为“ amount”的虚拟列的表。问题在于JSON并不总是包含键“金额”。有时它被称为“ presentationAmount”。
是否可以为此设置条件?
当JSON包含使用“ presentationAmount”的键“ threeDSecure”时,否则使用“ amount”。
这是我的创建表代码:
CREATE TABLE transactions (
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
json JSON DEFAULT NULL,
type VARCHAR(12) GENERATED ALWAYS AS (json->>"$.transaction.type"),
uuid VARCHAR(32) GENERATED ALWAYS AS (json->>"$.transaction.payload.id"),
holder VARCHAR(20) GENERATED ALWAYS AS (json->>"$.transaction.payload.card.holder"),
amount DECIMAL(11,2) GENERATED ALWAYS AS (json->>"$.transaction.payload.amount"),
resultCode VARCHAR(11) GENERATED ALWAYS AS (json->>"$.transaction.payload.result.code"),
processingTime DATETIME GENERATED ALWAYS AS (json->>"$.transaction.payload.timestamp"),
paymentType VARCHAR(2) GENERATED ALWAYS AS (json->>"$.transaction.payload.paymentType"),
paymentBrand VARCHAR(20) GENERATED ALWAYS AS (json->>"$.transaction.payload.paymentBrand"),
eci INT(2) GENERATED ALWAYS AS (json->>"$.transaction.payload.eci"),
recurringType VARCHAR(9) GENERATED ALWAYS AS (json->>"$.transaction.payload.recurringType"),
clearingInstitute VARCHAR(30) GENERATED ALWAYS AS (json->>"$.transaction.payload.resultDetails.clearingInstituteName"),
merchantTransactionId VARCHAR(64) GENERATED ALWAYS AS (json->>"$.transaction.payload.merchantTransactionId"),
divisionName VARCHAR(32) GENERATED ALWAYS AS (json->>"$.division.name"),
divisionUuid VARCHAR(32) GENERATED ALWAYS AS (json->>"$.division.uuid"),
merchantName VARCHAR(32) GENERATED ALWAYS AS (json->>"$.merchant.name"),
merchantUuid VARCHAR(32) GENERATED ALWAYS AS (json->>"$.merchant.uuid"),
channelName VARCHAR(32) GENERATED ALWAYS AS (json->>"$.channel.name"),
channelUuid VARCHAR(32) GENERATED ALWAYS AS (json->>"$.channel.uuid"),
INDEX typeIndex (type),
INDEX idIndex (uuid),
INDEX holderIndex (holder),
INDEX amountIndex (amount),
INDEX resultCodeIndex (resultCode),
INDEX timestampIndex (processingTime),
INDEX paymentTypeIndex (paymentType),
INDEX paymentBrandIndex (paymentBrand),
INDEX recurringTypeIndex (recurringType),
INDEX clearingInstituteIndex (clearingInstitute),
INDEX merchantTransactionIdIndex (merchantTransactionId),
INDEX divisonNameIndex (divisionName),
INDEX divisionUuidIndex (divisionUuid),
INDEX merchantNameIndex (merchantName),
INDEX merchantUuidindex (merchantUuid),
INDEX channelNameIndex (channelName),
INDEX channelUuidIndex (channelUuid)
) ENGINE=INNODB;
答案 0 :(得分:2)
您可以使用IF()
表达式。
amount DECIMAL(11,2) GENERATED ALWAYS AS
(IF(JSON_CONTAINS_PATH(json, 'one', '$.transaction.payload.threeDSecure'),
json->>"$.transaction.payload.presentationAmount",
json->>"$.transaction.payload.amount")),
有关其使用的详细信息,请参见JSON_CONTAINS_PATH()
的文档。