How to select values from JSON in mysql

后端 未结 2 738
一生所求
一生所求 2020-12-11 13:29

Hi can anyone tell me what is wrong with this query.

DECLARE @json LONGTEXT;

SET @json = \'[ { \"name\":\"John Smith\",  \"address\":\"780 Mission St, San          


        
2条回答
  •  一整个雨季
    2020-12-11 14:05

    First, in mysql you don't need to declare a variable, in this case. Just use 'SET' keyword. And finaly, you need put an alias for your 'select' query. Like this:

    SET @json = '[ { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}     ]';
    
    ##SELECT @json;
    
    SELECT * FROM JSON_TABLE (@json, '$[*]' COLUMNS (
                    `name` VARCHAR(40)  PATH '$.name',
                    `address` VARCHAR(100) PATH '$.address')) AS T;
    

提交回复
热议问题