合并/加入sql查询

bttbmeg0  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(291)

我有一个查询,它从json文档中获取数据,检查返回了哪些值,然后返回该计数。

SELECT 
     (CASE WHEN cat_name IS NOT NULL THEN 1 ELSE 0 END +
        CASE WHEN dog_name IS NOT NULL THEN 1 ELSE 0 END
       ) AS cat_dog_total 
from (select JSON_EXTRACT_SCALAR(data, '$.cat.name') as cat_name
             JSON_EXTRACT_SCALAR(data, '$.dog.name') as dog_name
      from table
     )

作为查询的一部分,我还想返回 cat_name , dog_name 并添加一个 WHERE pet_store = 'london' 我不确定应该在哪里添加其他参数,或者是否应该加入子查询?

j8ag8udp

j8ag8udp1#

SELECT cat_name, dog_name,
     (CASE WHEN cat_name IS NOT NULL THEN 1 ELSE 0 END +
        CASE WHEN dog_name IS NOT NULL THEN 1 ELSE 0 END
       ) AS cat_dog_total 
from (select JSON_EXTRACT_SCALAR(data, '$.cat.name') as cat_name
             JSON_EXTRACT_SCALAR(data, '$.dog.name') as dog_name
      from table
      WHERE pet_store = 'london'
     )

相关问题