Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 基于优先级的Case-SQL查询_Mysql_Sql - Fatal编程技术网

Mysql 基于优先级的Case-SQL查询

Mysql 基于优先级的Case-SQL查询,mysql,sql,Mysql,Sql,我有一个user表,其中包含user\u id和user\u details等列。在user_details列中,包含字符串格式的JSON数据,如下所示: "[{"value":"sachin","label":"What's your first name?"},{"value":"test@example.com","label":"What's your email?"},{"value":"+911234567890","label":"What's your phone number?

我有一个user表,其中包含user\u id和user\u details等列。在user_details列中,包含字符串格式的JSON数据,如下所示:

"[{"value":"sachin","label":"What's your first name?"},{"value":"test@example.com","label":"What's your email?"},{"value":"+911234567890","label":"What's your phone number?"},{"value":"xyz","label":"What's your city?"},{"value":"abc","label":"What's your address?"}]"
如上图所示,它将在user_details列的name和value对中包含数据

我需要一个sql查询,用于从user_details列中查找值,如果标签包含“name”或“email”或“phone number”,则显示相应的值。 示例-1:

"[{"value":"sachin","label":"What's your first name?"},{"value":"test@example.com","label":"What's your email?"},{"value":"+911234567890","label":"What's your phone number?"},{"value":"xyz","label":"What's your city?"},{"value":"abc","label":"What's your address?"}]"
在此示例中,SQL查询应生成以下输出:

名称| sachin

示例2:

"[{"value":"test@example.com","label":"What's your email?"},{"value":"+911234567890","label":"What's your phone number?"},{"value":"xyz","label":"What's your city?"},{"value":"abc","label":"What's your address?"}]"
在这方面,它应该是: 电子邮件|test@example.com

示例-3:

"[{"value":"+911234567890","label":"What's your phone number?"},{"value":"xyz","label":"What's your city?"},{"value":"abc","label":"What's your address?"}]"
在这方面,它应该是: 电话|+911234567890

示例4:

"[{"value":"xyz","label":"What's your city?"},{"value":"abc","label":"What's your address?"}]"
在这方面,它应该是: id | 5 (在本例中,我们没有姓名、电子邮件或电话号码,查询应返回id,即该行的主键。)


我尝试使用case查询,但似乎不起作用。有什么方法可以得到这个输出吗?

正如其他人所说的,您确实应该更改您的数据库模式。但是,如果您仍使用当前设置,此查询可能满足您的需要:

SELECT
  CASE WHEN json_search(user_details, 'one', '%name%', null, '$[*].label')
         IS NOT NULL
       THEN 'name'
       WHEN json_search(user_details, 'one', '%email%', null, '$[*].label') 
         IS NOT NULL
       THEN 'email'
       WHEN json_search(user_details, 'one', '%phone number%', null, '$[*].label') 
         IS NOT NULL
       THEN 'phone'
       ELSE 'id'
  END type,
  CASE WHEN json_search(user_details, 'one', '%name%', null, '$[*].label') 
         IS NOT NULL
       THEN json_unquote(json_extract(user_details, 
              concat(
                json_unquote(
                  replace(
                    json_search(user_details, 'one', '%name%', null, '$[*].label'), 
                    '.label', '')),
                '.value')))
       WHEN json_search(user_details, 'one', '%email%', null, '$[*].label') 
         IS NOT NULL
       THEN json_unquote(json_extract(user_details, 
              concat(
                json_unquote(
                  replace(
                    json_search(user_details, 'one', '%email%', null, '$[*].label'), 
                    '.label', '')),
                '.value')))

       WHEN json_search(user_details, 'one', '%phone number%', null, '$[*].label') 
         IS NOT NULL
       THEN json_unquote(json_extract(user_details, 
              concat(
                json_unquote(
                  replace(
                    json_search(user_details, 'one', '%phone%', null, '$[*].label'), 
                    '.label', '')),
                '.value')))
       ELSE user_id
  END value
FROM json_user;