在 JSON 中搜索准确的字符串值

Search for exact string value in JSON

我在 JSON 中存储了一个看起来像

的列

列名称:s2s_payload

值:

{ 
    "checkoutdate":"2019-10-31",
    "checkindate":"2019-10-30",
    "numtravelers":"2",
    "domain":"www.travel.com.mx",
    "destination": {
                       "country":"MX",
                       "city":"Manzanillo"
                   },
    "eventtype":"search",
    "vertical":"hotels"
}

我想查询数组中的确切值,而不是返回特定数据类型的所有值。我正在使用 JSON_EXTRACT 来获得不同的计数。

SELECT 
    COUNT(JSON_EXTRACT(s2s_payload, '$.destination.code')) AS total, 
    JSON_EXTRACT(s2s_payload, '$.destination.code') AS destination
FROM 
    "db"."events_data_json5_temp"
WHERE 
    id = '111000'
    AND s2s_payload IS NOT NULL
    AND yr = '2019'
    AND mon = '10'
    AND dt >= '26'
    AND JSON_EXTRACT(s2s_payload, '$.destination.code')
GROUP BY  
    JSON_EXTRACT(s2s_payload, '$.destination.code')

如果我想过滤 where ""eventtype"":""search"" 我该怎么做?

我尝试使用 CAST(s2s_payload AS CHAR) = '{"eventtype"":""search"}' 但那没有用。

您需要使用 json_extract + a CAST 来获取要比较的实际值:

CAST(json_extract(s2s_payload, '$.eventtype') AS varchar) = 'search'

或者,与 json_extract_scalar 相同(因此不需要 CAST):

json_extract_scalar(s2s_payload, '$.eventtype')