SQL @> JSON containment operator
payload @> '{"tags": ["pro"]}' is true when the right-hand document fits inside the left one. Containment, not equality, and GIN-indexable.
- 类别
- Strings
- Returns
boolean- In this category
- 151 of 172
语法
jsonb @> jsonb
jsonb <@ jsonb
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | Not built in | no equivalent short of OPENJSON shredding |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | Different name or syntax | JSON_CONTAINS(target, candidate) means the same |
| SQLite | Not built in | no operator; emulate with json_each |
| Oracle | Not built in | no operator; JSON_EXISTS for paths |
示例
PostgreSQL
SELECT * FROM events
WHERE payload @> '{"tags": ["pro"]}';
PostgreSQL
SELECT * FROM events WHERE payload->'user' @> '"matt"';
PostgreSQL
CREATE INDEX ON events USING gin (payload);
值得了解
- @> is containment, not equality: '{"a":1,"b":2}' @> '{"a":1}' is true. The reversed order asks the opposite question; <@ is contained-in.
- The operator exists for jsonb only: a json column or a text literal needs the ::jsonb cast, and the error names the types, not the fix.
- Paired with a GIN index (jsonb_ops or jsonb_path_ops) it answers from the index; without one, every row is parsed and compared.
- Array containment means all elements: tags @> '["a","b"]' requires both. For any-of matching on string elements, use ?|: tags ?| array['a', 'b'].
Questions about @> (jsonb)
@> or ->> comparisons?
@> for 'has this member/element' queries, indexable with GIN. ->> for comparing extracted text. Mixing them works but the index only helps @>.
Why does @> fail on my json column?
The containment operator is defined for jsonb. Cast both sides (::jsonb) or, better, migrate the column to jsonb; json cannot use GIN indexes either.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.