2

Using MySQL 5.7, I want to know the PostgreSQL equivalent of

SELECT * FROM user_conversations WHERE JSON_CONTAINS(users, JSON_ARRAY(1))

How do you write JSON_CONTAINS(users, JSON_ARRAY(1)) in PostgreSQL

EDIT 1

there is my json it's just an array without son object :

[
 "john", 
 "doe", 
 "does"
]

i want to get "doe" for exemple

EDIT 2

My table :

  Column   |              Type              |                     Modifiers                     | Storage | Statistics Target | Description 
 ------------+--------------------------------+-------------------------------------------------------+----------+-----------------------+-------------
  id         | uuid                           | non NULL                                                   | plain    |                       | 
  name       | character varying(255)         | non NULL                                              | extended |                       | 
  image      | character varying(255)         | non NULL Par défaut, 'default.png'::character varying | extended |                       | 
  users      | json                          | non NULL                                              | extended |                       | 
  type       | integer                        | non NULL                                              | plain    |                       | 
  emoji_id   | integer                        | non NULL Par défaut, 0                                | plain    |                       | 
  created_at | timestamp(0) without time zone |                                                       | plain    |                       | 
  updated_at | timestamp(0) without time zone |                 

EDIT 3: I use laravel to execute queries :

DB::table('user_conversations')->whereRaw('JSON_CONTAINS(users, JSON_ARRAY(1))')->orderBy('created_at', 'desc')->paginate(5);

and it's work with mysql.

4
  • have you looked at the library. btw i have never used the mysql version nor this pgsql one. just thought that may be a good place to start Commented Dec 20, 2016 at 17:33
  • This question may be your ticket as well Commented Dec 20, 2016 at 17:36
  • Can you paste the schema for user_conversations? Commented Dec 20, 2016 at 18:13
  • @EvanCarroll see my last edit Commented Dec 20, 2016 at 18:24

1 Answer 1

4

The two argument form of MySQL's JSON_CONTAINS() you cite has a signature of JSON_CONTAINS(json_doc, val) That sounds similar to the PostgreSQL JSON operator @> operator

-- returns true
SELECT '["john","bob","doe","dylan","mike","does"]'::jsonb @>
  '["john","doe","does"]';

-- returns false
SELECT '["john","bob","doe","dylan","mike","does"]'::jsonb @>
  '["john","mary"]';

If your type is json, that's fine just cast it to jsonb;

SELECT *
FROM user_conversations
WHERE users::jsonb @> json_text;
Sign up to request clarification or add additional context in comments.

7 Comments

i use json type not jsonb
@yecir you shouldn't, but it works the same, updated the answer.
but i want : select * from table where (json column)
I'm not sure, it's not clear what you're asking. JSON is a text column, validated. JSONB is a binary type that you can operate on. Text has to become binary to operate on it.
SELECT * FROM user_conversations WHERE users::jsonb @> "4411167c-4af8-39e8-b49a-85de76dc38cb"; ERROR: column « 4411167c-4af8-39e8-b49a-85de76dc38cb » doesn't exists
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.