从JSON数据类型特定值中的Postgres数据库中获取行

时间:2019-01-23 05:56:14

标签: php mysql postgresql codeigniter

您好,我从Json列中提取行时遇到了问题。
    下面是json数据,如果要从中提取行,请从值中将其表示为MH。

以下各行中的数据。因此,如果我查询此适用性列的MH是否需要获取行,则需要一个代码初始化查询。
    你能帮我这个忙吗?

以下是JSON数据:String.format("%-" + 3 + "." + 3 + "s", givenString);

["AP","AR","AS","BR","CT","GA","GJ","HR","HP","JK","JH","KA","KL","MP","MH","MN","ML","MZ","NL","OR","PB","RJ","SK","TN","TG","TR","UT","UP","WB","AN","CH","DN","DD","DL","LD","PY"]

该表结构如下所示。

2 个答案:

答案 0 :(得分:0)

访问以下站点

访问http://www.postgresqltutorial.com/postgresql-json/

然后从

更改您的代码
$this->select('guid, applicability, name, type, description,
service_type, scheme_type, mini_description, tags, benefit');
$this->join('schemes_lang_1', 'schemes_lang_1.scheme_id=scheme_1.id', 'left');
$this->where('lang', $lang);
$this->where('status', $status);
$this->where('scheme_1.pp_enabled', 1);
$this->where('scheme_1.applicability', 'MH');
return $this->findAll();

$query = $this->db->query("YOUR POSTGRES QUERY");

foreach ($query->result_array() as $row)
{
  echo $row['guid'];
  echo $row['applicability'];
  echo $row['name'];
}

答案 1 :(得分:0)

下面给出的是行查询,您可以将此查询用作参考并添加所需的任意条件

SELECT
  guid, 
  applicability, 
  name, 
  type, 
  description,
  service_type, 
  scheme_type, 
  mini_description, 
  tags, 
  benefit
FROM scheme_1
LEFT JOIN schemes_lang_1 ON schemes_lang_1.scheme_id=scheme_1.id
WHERE
  (scheme_1.applicability)::jsonb ? 'MH';

您还可以通过json键(例如 如果json是这样

["company":"AP","Detail":"test"]

使用(scheme_1.applicability->'company')::jsonb ? 'AP';

在Codeignitor中,您可以这样

$this->select('guid, applicability, name, type, description,
service_type, scheme_type, mini_description, tags, benefit');
$this->join('schemes_lang_1', 'schemes_lang_1.scheme_id=scheme_1.id', 'left');
$this->where('lang', $lang);
$this->where('status', $status);
$this->where('scheme_1.pp_enabled', 1);
$this->where('(scheme_1.applicability)::jsonb ? ', 'MH');

return $this->findAll();

注意:它比使用循环快得多,然后在表字段中逐一搜索记录,而循环搜索可能会使代码的时间和空间复杂度增加1000倍。