Bagaimana cara menggunakan json di mysql?

Pada saat ini, Anda mungkin bertanya pada diri sendiri mengapa Anda ingin menggunakan JSON ketika MySQL telah melayani berbagai macam kebutuhan database bahkan sebelum memperkenalkan tipe data JSON

Jawabannya terletak pada kasus penggunaan di mana Anda mungkin akan menggunakan pendekatan make-shift

Mari saya jelaskan dengan sebuah contoh

Misalkan Anda sedang membangun aplikasi web di mana Anda harus menyimpan konfigurasi/preferensi pengguna di database

Umumnya, Anda dapat membuat tabel database terpisah dengan bidang

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
1,
/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
2,
/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
3, dan
/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
4 atau menyimpannya sebagai string berformat yang dapat Anda parse saat waktu proses

Namun, ini bekerja dengan baik untuk sejumlah kecil pengguna. Jika Anda memiliki sekitar seribu pengguna dan lima kunci konfigurasi, Anda sedang melihat tabel dengan lima ribu catatan yang membahas fitur yang sangat kecil dari aplikasi Anda

Atau jika Anda mengambil rute string yang diformat, kode asing yang hanya menambah beban server Anda

Menggunakan bidang tipe data JSON untuk menyimpan konfigurasi pengguna dalam skenario seperti itu dapat menghemat ruang tabel database dan menurunkan jumlah catatan, yang disimpan secara terpisah, menjadi sama dengan jumlah pengguna

Dan Anda mendapatkan manfaat tambahan karena tidak perlu menulis kode parsing JSON apa pun, ORM atau runtime bahasa akan menanganinya

Operasi CRUD

Mari kita lihat cara membuat, membaca, memperbarui, dan menghapus data di bidang JSON

Membuat

Membuat catatan dalam database dengan bidang JSON cukup sederhana

Yang perlu Anda lakukan hanyalah menambahkan JSON yang valid sebagai nilai bidang dalam pernyataan penyisipan Anda

/* Let's sell some televisions */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Prime' ,
'1' ,
'1' ,
'{"screen": "50 inch", "resolution": "2048 x 1152 pixels", "ports": {"hdmi": 1, "usb": 3}, "speakers": {"left": "10 watt", "right": "10 watt"}}'
);
_

Alih-alih menata sendiri objek JSON, Anda juga dapat menggunakan fungsi

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
5 bawaan

Fungsi

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
5 menerima daftar pasangan kunci/nilai dalam bentuk
/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
7 dan mengembalikan objek JSON

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);

Perhatikan fungsi

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
8 yang mengembalikan larik JSON saat meneruskan serangkaian nilai

Jika Anda menentukan satu kunci beberapa kali, hanya pasangan kunci/nilai pertama yang akan dipertahankan. Ini disebut normalisasi JSON dalam istilah MySQL. Selain itu, sebagai bagian dari normalisasi, kunci objek diurutkan dan ruang kosong ekstra antara pasangan kunci/nilai dihapus

Fungsi lain yang bisa kita gunakan untuk membuat objek JSON adalah fungsi

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
9

Fungsi

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
9 mengambil beberapa objek JSON dan menghasilkan objek agregat tunggal

/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);

Membaca

Saat ini, kami memiliki beberapa produk di database kami untuk dikerjakan

Untuk nilai khas MySQL yang bukan tipe JSON, klausa where cukup mudah. Cukup tentukan kolom, operator, dan nilai yang perlu Anda kerjakan

Secara heuristik, saat bekerja dengan kolom JSON, ini tidak berfungsi

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';

Saat Anda ingin mempersempit baris menggunakan bidang JSON, Anda harus terbiasa dengan konsep ekspresi jalur

Definisi ekspresi jalur yang paling sederhana (pikirkan pemilih JQuery) digunakan untuk menentukan bagian mana dari dokumen JSON yang akan digunakan

Bagian kedua dari teka-teki ini adalah fungsi

/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
_1 yang menerima ekspresi jalur untuk bernavigasi melalui JSON

Katakanlah kami tertarik pada rangkaian televisi yang memiliki setidaknya satu port USB dan HDMI

SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;

Argumen pertama untuk fungsi

/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
1 adalah JSON untuk menerapkan ekspresi jalur yang merupakan kolom
/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
3. Simbol
/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
_4 menandai objek untuk dikerjakan. Ekspresi jalur
/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
5 dan
/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
6 masing-masing diterjemahkan menjadi "ambil kunci usb di bawah port" dan "ambil kunci HDMI di bawah port"

Setelah kami mengekstrak kunci yang kami minati, cukup mudah untuk menggunakan operator MySQL seperti

/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
7 pada mereka

Selain itu, fungsi

/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
_1 memiliki alias
/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
9 yang dapat Anda gunakan untuk membuat kueri Anda lebih mudah dibaca

merevisi permintaan kami sebelumnya

SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND `attributes` -> '$.ports.usb' > 0
AND `attributes` -> '$.ports.hdmi' > 0;

Memperbarui

Untuk memperbarui nilai JSON, kita akan menggunakan fungsi

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
0,
/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
1, dan
/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
2. Fungsi ini juga memerlukan ekspresi jalur untuk menentukan bagian mana dari objek JSON yang akan dimodifikasi

Keluaran dari fungsi ini adalah objek JSON yang valid dengan perubahan yang diterapkan

Mari kita modifikasi semua ponsel untuk memiliki properti chipset juga

UPDATE `e_store`.`products`
SET `attributes` = JSON_INSERT(
`attributes` ,
'$.chipset' ,
'Qualcomm'
)
WHERE
`category_id` = 2;

Ekspresi jalur

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
_3 mengidentifikasi posisi properti
/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
4 berada di akar objek

Mari kita perbarui properti

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
_4 menjadi lebih deskriptif menggunakan fungsi
/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
1

UPDATE `e_store`.`products`
SET `attributes` = JSON_REPLACE(
`attributes` ,
'$.chipset' ,
'Qualcomm Snapdragon'
)
WHERE
`category_id` = 2;

Mudah sekali

Terakhir, kami memiliki fungsi

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
_2 yang akan kami gunakan untuk menentukan televisi kami cukup berwarna

UPDATE `e_store`.`products`
SET `attributes` = JSON_SET(
`attributes` ,
'$.body_color' ,
'red'
)
WHERE
`category_id` = 1;

Semua fungsi ini tampak identik tetapi ada perbedaan dalam perilakunya

Fungsi

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
_0 hanya akan menambahkan properti ke objek jika belum ada

Fungsi

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
_1 menggantikan properti hanya jika ditemukan

Fungsi

/* It's not that simple */
SELECT
*
FROM
`e_store`.`products`
WHERE
attributes = '{"ports": {"usb": 3, "hdmi": 1}, "screen": "50 inch", "speakers": {"left": "10 watt", "right": "10 watt"}, "resolution": "2048 x 1152 pixels"}';
2 akan menambahkan properti jika tidak ditemukan atau menggantinya

Menghapus

Ada dua bagian untuk menghapus yang akan kita lihat

Yang pertama adalah menghapus kunci/nilai tertentu dari kolom JSON Anda sedangkan yang kedua adalah menghapus baris menggunakan kolom JSON

Katakanlah kami tidak lagi memberikan informasi

SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;
1 untuk kamera dan ingin menghapusnya untuk semua kamera

Kami akan melakukannya menggunakan fungsi

SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;
_2 yang mengembalikan JSON yang diperbarui setelah menghapus kunci yang ditentukan berdasarkan ekspresi jalur

UPDATE `e_store`.`products`
SET `attributes` = JSON_REMOVE(`attributes` , '$.mount_type')
WHERE
`category_id` = 3;

Untuk kasus kedua, kami juga tidak menyediakan lagi ponsel yang memiliki OS Android versi Jellybean

/* Let's sell some mobilephones */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Desire' ,
'2' ,
'2' ,
JSON_OBJECT(
"network" ,
JSON_ARRAY("GSM" , "CDMA" , "HSPA" , "EVDO") ,
"body" ,
"5.11 x 2.59 x 0.46 inches" ,
"weight" ,
"143 grams" ,
"sim" ,
"Micro-SIM" ,
"display" ,
"4.5 inches" ,
"resolution" ,
"720 x 1280 pixels" ,
"os" ,
"Android Jellybean v4.3"
)
);
0

Seperti yang dinyatakan sebelumnya, bekerja dengan atribut tertentu memerlukan penggunaan fungsi

/* Let's sell some cameras */
INSERT INTO `e_store`.`products`(
`name` ,
`brand_id` ,
`category_id` ,
`attributes`
)
VALUES(
'Explorer' ,
'3' ,
'3' ,
JSON_MERGE(
'{"sensor_type": "CMOS"}' ,
'{"processor": "Digic DV III"}' ,
'{"scanning_system": "progressive"}' ,
'{"mount_type": "PL"}' ,
'{"monitor_type": "LCD"}'
)
);
1 sehingga untuk menerapkan operator
SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;
4, pertama-tama kami telah mengekstrak properti
SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;
5 ponsel (dengan bantuan
SELECT
*
FROM
`e_store`.`products`
WHERE
`category_id` = 1
AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0
AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;
6) dan menghapus semua catatan yang berisi string

Bagaimana cara menggunakan JSON di MySQL?

Menambahkan Data JSON .
JSON_ARRAY(), yang membuat array. Sebagai contoh. -- mengembalikan [1, 2, "abc"]. SELECT JSON_ARRAY(1, 2, 'abc');
Fungsi JSON_OBJECT(), yang membuat objek. Sebagai contoh. .
Fungsi JSON_QUOTE(), yang mengutip string sebagai nilai JSON. Sebagai contoh. .
atau Anda bisa (CAST anyValue AS JSON)

Bagaimana cara menampilkan data JSON di MySQL?

Untuk mendemonstrasikan penggunaan operator ini, kita perlu memiliki tabel dengan bidang JSON. .
Gunakan $. .
Gunakan $[indeks] untuk mengekstrak nilai elemen dari larik JSON
Gunakan -> sebagai jalan pintas untuk JSON_EXTRACT jika nilainya bukan string

Kapan menggunakan JSON di MySQL?

MySQL mendukung tipe data JSON asli sejak versi 5. 7. 8. Tipe data JSON asli memungkinkan Anda menyimpan dokumen JSON lebih efisien daripada format teks JSON di versi sebelumnya . MySQL menyimpan dokumen JSON dalam format internal yang memungkinkan akses baca cepat ke elemen dokumen.

Bagaimana cara memuat file JSON di MySQL?

Impor JSON .
Putuskan ke tabel mana data akan diimpor. .
Pilih format impor JSON dan tentukan lokasi data Sumber. .
Tentukan koneksi MySQL, database, skema, dan tabel untuk mengimpor data. .
Pratinjau data Sumber dan tentukan opsi tambahan untuk menyesuaikan impor