문제

I am currently using postgres 9.3.3

Following is how my table looks like -

 Column       |           Type           |                             Modifiers                              | Storage  | Stats target | Description 
--------------------+--------------------------+--------------------------------------------------------------------+----------+--------------+-------------
 id                 | integer                  | not null default nextval('playerbase_palyerdata_id_seq'::regclass) | plain    |              | 
 date_joined        | timestamp with time zone | not null                                                           | plain    |              | 
 belongs_to_camp_id  | integer                  | not null                                                           | plain    |              | 
 belongs_to_coach_id | integer                  | not null                                                           | plain    |              | 
 json_kvps          | character varying(2000)  | not null                                                           | extended |              | 

One sample data is as follows -

id |      date_joined   | belongs_to_camp_id | belongs_to_coach_id | json_kvps

1  | 2014-03-07 18:10:45.824749+05:30 |                 1 |                  1 | {"alumnicode": "2003360009", "emailusername": "aaron@hotmail.com", "altemail": "", "salutation": "Mrs", "fname": "Aaron", "mname": "V", "lname": "Schwartz", "fullname": "Aaraon M Scwartz", "programmename": "MEP", "batchyearin": "2003"}

Now I want to search the entire table, and find a user with "emailusername":"aaron@hotmail.com"

As mentioned here - http://www.postgresql.org/docs/current/static/functions-json.html

I try to write a query as follows -

SELECT * FROM playerbase_playerdata WHERE json_kvps->>'emailusername' = 'aaron@hotmail.com';

I was expecting a column as above, instead I got the following error -

ERROR:  operator does not exist: character varying ->> unknown
LINE 1: ...ELECT * FROM memberbase_memberdata WHERE json_kvps->>'emailu...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Could someone please tell me, what am I missing?

도움이 되었습니까?

해결책

Feature like this exists only from PostgreSQL release 9.3. So, unfortunately, to make queries like this you need to update your PostgreSQL.

If you have 9.3, then you need use json column type.

Here you can see some example. It helps me, earlier: http://clarkdave.net/2013/06/what-can-you-do-with-postgresql-and-json/

Have a nice day.

다른 팁

For PostgreSQL 9.3 or higher, one possible solution could be using a cast to json:

SELECT * FROM playerbase_playerdata WHERE json_kvps->>'emailusername' = 'aaron@hotmail.com';

SELECT * FROM playerbase_playerdata WHERE CAST(json_kvps AS JSON)->>'emailusername' = 'aaron@hotmail.com';

In case of django models you can use JsonField.

https://pypi.python.org/pypi/jsonfield.

It works fine, but if you'll use PostgreSQL version less than 9.2, it'll create character column.

select [Required Fields] from  [Table Name] WHERE  [Column Name]::text = '{[Key]}' ::text
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top