Postgres

So, I'm trying to query postgres using:

$connection = $this->db;
$data = $connection->query($sql);
$results = $data->fetchAll();

This works wonders except when I try to use json fields. Usually, I find some kind of workaround but honestly I would like to solve this without having to apply tricks to my sql query.

I'd like to run this query:

SELECT *
FROM "event" AS e
WHERE value ? 'campaign_code'

It queries for all events returning those with the key campaign_code present in the value column (jsonb field)

When I try to run this, the "?" is interpretad as a binding and shouldn't (in this case), causing me an error.

SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "$1"
LINE 23:      WHERE value $1 'campaign_code'

Any thoughts?



4.0k

use jsonb_exists(value, 'campaign_code')



469

Is that documented somewhere? I must have missed it.



4.0k

I found it in stack overflow, but I can't find any documents in postgresql.org



469

Btw, have you found an equivalent for "@>"?



4.0k

maybe jsonb_contain? or jsonb_contained?