MySQL-基于条件创建具有虚拟列的表

MySQL-基于条件创建具有虚拟列的表,mysql,Mysql,我试图创建一个包含引用JSON的索引虚拟列的表 我创建了一个表,其中包含一个名为“amount”的虚拟列。问题是JSON并不总是包含键“amount”。有时它被命名为“presentationAmount” 有没有可能为此设定一个条件 当JSON包含键“threeDSecure”时,即使用“presentationAmount”,否则使用“amount” 以下是我的创建表代码: CREATE TABLE transactions ( id BIGINT(20) NOT NULL AUTO

我试图创建一个包含引用JSON的索引虚拟列的表

我创建了一个表,其中包含一个名为“amount”的虚拟列。问题是JSON并不总是包含键“amount”。有时它被命名为“presentationAmount”

有没有可能为此设定一个条件

当JSON包含键“threeDSecure”时,即使用“presentationAmount”,否则使用“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;
可以使用
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\u CONTAINS\u PATH()。它在文档中。