无法获取变量为JSON键的Mysql json值

时间:2018-12-10 06:35:50

标签: mysql json select

SET @im := (SELECT items->"$.one" From fruits where id = 12);
//it's working and saved result into @im variable

但是我需要使用函数中的变量来获取它 我尝试了但是失败了.. 我尝试了concat,它也行不通, 我试过准备执行stmt,它也无法正常工作。

SET @p := "one";
SET @im := (SELECT items->"$."@p From fruits where id = 12);
//it's not working


"$.one" ==> "$."@p
//difference..

请帮助我,如何获得此信息。

1 个答案:

答案 0 :(得分:0)

尝试这种方法应该可以。

SET @p = 'one';
SET @sql = '(SELECT items->"$.@p" into @im From fruits where id = 12)';
SET @sql = replace(@sql,'@p',@p);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
select @im;