Как выбрать строки, содержащие определенный ключ столбца JSON, независимо от значения. Все на Laravel и MySQL.Php

Кемеровские программисты php общаются здесь
Гость
Как выбрать строки, содержащие определенный ключ столбца JSON, независимо от значения. Все на Laravel и MySQL.

Сообщение Гость »


Я написал этот код в Laravel 10:

Код: Выделить всё

$unit = Unit::whereJsonContains('prefixes->abc', 'cba')->first();
And I have this query:

Код: Выделить всё

select *
from `units`
where json_contains(`prefixes`, '\"cba\"', '$."abc"')
limit 1
I have a row in the database that contains

Код: Выделить всё

{ "abc": "cba", "cde": 100 }
JSON in the 'prefixes' column.
My code works fine - it selects the correct row from the database.
I want to select a row that contains a key named "abc" regardless of the value of that key. I can't achieve this.
I thought of making a selection like this:

Код: Выделить всё

select *
from `units`
where json_contains(`prefixes`, '%', '$."abc"')
limit 1
but that doesn't work in phpadmin (it doesn't select anything).
I have not been able to get the right query through either Laravel Eloquent or raw SQL selection.
What should I do in Laravel and what should the SQL query look like?


Источник: https://stackoverflow.com/questions/781 ... f-the-valu

Вернуться в «Php»