2017-12-05 5 views
0

Ich habe eine Tabelle, Struktur wie folgt.JSON_EXTRACT mehrdimensionale Daten von MySQL

CREATE TABLE `layout` (
    `id` int(11) NOT NULL, 
    `title` varchar(100) NOT NULL, 
    `slug` varchar(100) NOT NULL, 
    `structure` json NOT NULL, 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

Das Feld mit dem Namen structure, hält wie unten Daten.

[{"Field":"Name","Type":"Text"},{"Field":"DOB","Type":"Date"}] 

Ich möchte Abfrage ausführen, wo ich "Feld" -Wert geben und ich bekomme "Typ" -Wert.

Antwort

0

Versuchen:

SET @`Field` := 'Name'; 

SELECT 
    `id`, 
    JSON_UNQUOTE(
    JSON_EXTRACT(
     `structure`, 
     JSON_UNQUOTE(
     REPLACE(
      JSON_SEARCH(
      `structure`, 
      'one', 
      @`Field`, 
      NULL, 
      '$[*].Field' 
     ), 
      'Field', 
      'Type' 
     ) 
    ) 
    ) 
) 
FROM 
    `layout`; 

db-fiddle See.

Verwandte Themen