<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MYSQL - Hi You, I am Tade</title>
	<atom:link href="https://hoangthuc.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>https://hoangthuc.com/category/mysql/</link>
	<description></description>
	<lastBuildDate>Wed, 15 Mar 2023 03:48:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://hoangthuc.com/wp-content/uploads/2024/02/logoTH-2-150x150.png</url>
	<title>MYSQL - Hi You, I am Tade</title>
	<link>https://hoangthuc.com/category/mysql/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Dữ liệu JSON trong MySQL</title>
		<link>https://hoangthuc.com/du-lieu-json-trong-mysql/</link>
					<comments>https://hoangthuc.com/du-lieu-json-trong-mysql/#comments</comments>
		
		<dc:creator><![CDATA[Hoàng Thức]]></dc:creator>
		<pubDate>Tue, 12 Jan 2021 09:41:28 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Lập trình]]></category>
		<category><![CDATA[MYSQL]]></category>
		<guid isPermaLink="false">https://hoangthuc.com/?p=719</guid>

					<description><![CDATA[<p>Bắt đầu từ Mysql 5.7.8, Mysql hỗ trợ kiểu dữ liệu JSON được xác định nghĩa bởi RFC 7159 , cho phép truy cập vào dữ liệu trong các tài liệu JSON (Javascript Notation Object). Các điểm lợi của việc lưu dữ liệu vào cột dạng JSON: Tự động xác thực các dữ liệu dạng JSON Tối [&#8230;]</p>
<p>The post <a href="https://hoangthuc.com/du-lieu-json-trong-mysql/">Dữ liệu JSON trong MySQL</a> appeared first on <a href="https://hoangthuc.com">Hi You, I am Tade</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><span style="color: #000000;">Bắt đầu từ Mysql 5.7.8, Mysql hỗ trợ kiểu dữ liệu <strong>JSON</strong> được xác định nghĩa bởi <a style="color: #000000;" href="https://tools.ietf.org/html/rfc7159">RFC 7159</a> , cho phép truy cập vào dữ liệu trong các tài liệu <strong>JSON</strong> (Javascript Notation Object).</span><br />
<span style="color: #000000;">Các điểm lợi của việc lưu dữ liệu vào cột dạng <strong>JSON</strong>:</span></p>
<ul>
<li><span style="color: #000000;">Tự động xác thực các dữ liệu dạng JSON</span></li>
<li><span style="color: #000000;">Tối ưu hoá định dạng lưu trữ</span></li>
<li><span style="color: #000000;">Các hàm trong <strong>SQL</strong> hỗ trợ việc thao tác với dữ liệu cột <strong>JSON</strong> như tạo mới, quản lý hay tìm kiếm</span></li>
<li><span style="color: #000000;">Chuẩn hoá, hợp nhất dữ liệu JSON.</span></li>
</ul>
<h2><strong><span style="color: #000000;">Tạo Dữ Liệu JSON</span></strong></h2>
<p><span style="color: #000000;">Dữ liệu dạng <strong>JSON</strong> có 2 dạng phổ biến là Mảng <strong>JSON(Array Json)</strong> và Đối tượng <strong>JSON(Object Json)</strong>. Mảng Json chứa các giá trị được phân tách với nhau bởi dấu , và được đặt bên trong [ và ] :</span></p>
<pre><pre class="brush: sql; title: ; notranslate">&#x5B;&quot;abc&quot;, 10, null, true, false]</pre>
<p><span style="color: #000000;">Đối tượng Json chứa tập hợp các cặp khoá &#8211; giá trị (key &#8211; value) được phân tách với nhau cũng bởi dấu , và được đặt bên trong { và }:</span></p>
<pre><pre class="brush: xml; title: ; notranslate">{&quot;k1&quot;: &quot;value&quot;, &quot;k2&quot;: 10}</pre>
<p><span style="color: #000000;">Tuy nhiên với nhiều trường hợp phức tạp, dữ liệu <strong>JSON</strong> còn có dạng lồng nhau (Nesting):</span></p>
<pre><pre class="brush: xml; title: ; notranslate">&#x5B;99, {&quot;id&quot;: &quot;HK500&quot;, &quot;cost&quot;: 75.99}, &#x5B;&quot;hot&quot;, &quot;cold&quot;]]
{&quot;k1&quot;: &quot;value&quot;, &quot;k2&quot;: &#x5B;10, 20]}</pre>
<p><span style="color: #000000;">trong mảng <strong>JSON</strong> chứ đối tượng <strong>JSON</strong> và ngược lại.</span></p>
<p><span style="color: #000000;">Thay vì tự đặt ra đối tượng JSON, bạn cũng có thể sử dụng <strong>JSON_OBJECT</strong> chức năng tích hợp.</span></p>
<p><span style="color: #000000;">Các <strong>JSON_OBJECT</strong> chức năng chấp nhận một danh sách các cặp khóa / giá trị theo hình thức <strong>JSON_OBJECT(key1, value1, key2, value2, &#8230; key(n), value(n)) </strong>và trả về một đối tượng JSON.</span></p>
<pre><pre class="brush: sql; title: ; notranslate">/* Let's sell some mobilephones */

INSERT INTO `e_store`.`products`( `name` , `brand_id` , `category_id` , `attributes` )

VALUES( 'Desire' , '2' , '2' ,

JSON_OBJECT( &quot;network&quot; , JSON_ARRAY(&quot;GSM&quot; , &quot;CDMA&quot; , &quot;HSPA&quot; , &quot;EVDO&quot;) ,

&quot;body&quot; ,

&quot;5.11 x 2.59 x 0.46 inches&quot; ,

&quot;weight&quot; ,

&quot;143 grams&quot; ,

&quot;sim&quot; ,

&quot;Micro-SIM&quot; ) );

</pre>
<p><span style="color: #000000;">Một chức năng khác mà chúng ta có thể sử dụng để tạo các đối tượng JSON là <strong>JSON_MERGE</strong> hàm.</span></p>
<p><span style="color: #000000;">Các <strong>JSON_MERGE</strong> chức năng có nhiều đối tượng JSON và tạo ra một, tổng hợp đối tượng duy nhất.</span></p>
<pre><pre class="brush: xml; title: ; notranslate">INSERT INTO `e_store`.`products`( `name` , `brand_id` , `category_id` , `attributes` )

VALUES( 'Explorer' , '3' , '3' ,

JSON_MERGE(

'{&quot;sensor_type&quot;: &quot;CMOS&quot;}' ,

'{&quot;processor&quot;: &quot;Digic DV III&quot;}' ,

'{&quot;scanning_system&quot;: &quot;progressive&quot;}' ,

'{&quot;mount_type&quot;: &quot;PL&quot;}' ,

'{&quot;monitor_type&quot;: &quot;LCD&quot;}'

) );</pre>
<h2><strong><span style="color: #000000;">Thao Tác với Dữ Liệu Cột JSON</span></strong></h2>
<ul>
<li><span style="color: #000000;">Hãy tưởng tượng khi bạn muốn lấy một giá trị nào đó trong một cột dữ liệu <strong>JSON</strong>(ở đây lấy giá trị name):</span></li>
</ul>
<pre><pre class="brush: php; title: ; notranslate"> SELECT JSON_EXTRACT('{&quot;id&quot;: 14, &quot;name&quot;: &quot;Aztalan&quot;}', '$.name');
+---------------------------------------------------------+
| JSON_EXTRACT('{&quot;id&quot;: 14, &quot;name&quot;: &quot;Aztalan&quot;}', '$.name') |
+---------------------------------------------------------+
| &quot;Aztalan&quot;                                               |
+---------------------------------------------------------+
</pre>
<p><span style="color: #000000;">Ở đây <strong>Mysql</strong> cung cấp hàm <strong>JSON_EXTRACT</strong>() giúp trả về dữ liệu từ một tài liệu JSON. Dữ liệu được trả về dựa theo đường dẫn cung cấp, trong ví dụ trên chúng ta thấy đường dẫn ở đây là $.name.</span><br />
<span style="color: #000000;">Tìm hiểu sâu hơn về đường dẫn này nhé: $ là kí tự thể hiện tài liệu JSON đang được xét đến , name chính là tên khoá ta muốn lấy. Tuy nhiên với các dạng dữ liệu <strong>JSON</strong> thì đường dẫn lại khác nhau.</span><br />
<span style="color: #000000;">Với dạng Mảng <strong>JSON </strong>thì đường dẫn có dạng $[n] còn với Đối tượng <strong>JSON </strong>là $.key, n là vị trí giá trị chúng ta muốn lấy trong mảng và key là tên khoá của giá trị chúng ta muốn lấy.</span></p>
<pre><pre class="brush: php; title: ; notranslate"> SELECT JSON_EXTRACT('&#x5B;&quot;abc&quot;, 14, &quot;Aztalan&quot;]', '$&#x5B;0]');
+---------------------------------------------------------+
| JSON_EXTRACT('&#x5B;&quot;abc&quot;, 14, &quot;Aztalan&quot;]', '$&#x5B;0]')          |
+---------------------------------------------------------+
| &quot;abc&quot;                                                   |
+---------------------------------------------------------+
</pre>
<p><span style="color: #000000;">Tuy nhiên với trường hợp dữ liệu dạng lồng nhau thì đường dẫn của chúng ta không đơn giản như vậy. Cùng xem ví dụ sau nhé:</span></p>
<pre><pre class="brush: xml; title: ; notranslate">&#x5B;3, {&quot;a&quot;: &#x5B;5, 6], &quot;b&quot;: 10}, &#x5B;99, 100]]</pre>
<p><span style="color: #000000;">Đây là một mảng <strong>JSON</strong> vậy nếu chúng ta muốn lấy giá trị 3 trong mảng thì đường dẫn sẽ là $[0] (trong một array vị trí đầu tiên sẽ bắt đầu là 0), nhưng nếu chọn lấy giá trị thứ 2 ta sẽ có một đối tượng <strong>JSON </strong>{&#8220;a&#8221;: [5, 6], &#8220;b&#8221;: 10} vậy nếu bài toán đặt ra là muốn lấy giá trị của khoá b trong đối tượng <strong>JSON</strong> này thì ta phải có đường dẫn như nào?. Để lấy giá trị của khoá b ta sẽ có đường dẫn là $[1].b. Với tuỳ trường hợp dạng dữ liệu <strong>JSON</strong> ta sẽ có đường dẫn là $[n].key với dạng mảng lồng object <strong>JSON</strong> và $.key[n] mới dạng object lồng mảng <strong>JSON</strong>.</span></p>
<ul>
<li><span style="color: #000000;">Một số hàm phổ biến được Mysql hỗ trợ như <strong>JSON_SET, JSON_INSERT, JSON_REPLACE hay JSON_REMOVE.</strong></span></li>
</ul>
<pre><pre class="brush: php; title: ; notranslate"> SET @j = '&#x5B;&quot;a&quot;, {&quot;b&quot;: &#x5B;true, false]}, &#x5B;10, 20]]';</pre>
<p><span style="color: #000000;"><strong>JSON_SET</strong> () thay thế các giá trị cho các đường dẫn tồn tại và thêm các giá trị cho các đường dẫn không tồn tại:</span></p>
<pre><pre class="brush: php; title: ; notranslate"> SELECT JSON_SET(@j, '$&#x5B;1].b&#x5B;0]', 1, '$&#x5B;2]&#x5B;2]', 2);
+--------------------------------------------+
| JSON_SET(@j, '$&#x5B;1].b&#x5B;0]', 1, '$&#x5B;2]&#x5B;2]', 2) |
+--------------------------------------------+
| &#x5B;&quot;a&quot;, {&quot;b&quot;: &#x5B;1, false]}, &#x5B;10, 20, 2]]      |
+--------------------------------------------+</pre>
<p><span style="color: #000000;"><strong>JSON_INSERT</strong> () thêm các giá trị mới nhưng không thay thế các giá trị hiện có:</span></p>
<pre><pre class="brush: php; title: ; notranslate"> SELECT JSON_INSERT(@j, '$&#x5B;1].b&#x5B;0]', 1, '$&#x5B;2]&#x5B;2]', 2);
+-----------------------------------------------+
| JSON_INSERT(@j, '$&#x5B;1].b&#x5B;0]', 1, '$&#x5B;2]&#x5B;2]', 2) |
+-----------------------------------------------+
| &#x5B;&quot;a&quot;, {&quot;b&quot;: &#x5B;true, false]}, &#x5B;10, 20, 2]]      |
+-----------------------------------------------+</pre>
<p><span style="color: #000000;"><strong>JSON_REPLACE</strong> () thay thế các giá trị hiện có và bỏ qua các giá trị mới:</span></p>
<pre><pre class="brush: php; title: ; notranslate"> SELECT JSON_REPLACE(@j, '$&#x5B;1].b&#x5B;0]', 1, '$&#x5B;2]&#x5B;2]', 2);
+------------------------------------------------+
| JSON_REPLACE(@j, '$&#x5B;1].b&#x5B;0]', 1, '$&#x5B;2]&#x5B;2]', 2) |
+------------------------------------------------+
| &#x5B;&quot;a&quot;, {&quot;b&quot;: &#x5B;1, false]}, &#x5B;10, 20]]             |
+------------------------------------------------+</pre>
<p><span style="color: #000000;"><strong>JSON_REMOVE</strong> () lấy một tài liệu <strong>JSON</strong> và một hoặc nhiều đường dẫn chỉ định các giá trị sẽ bị xóa khỏi tài liệu. Giá trị trả về là tài liệu gốc trừ đi các giá trị được chọn bởi các đường dẫn tồn tại trong tài liệu:</span></p>
<pre><pre class="brush: php; title: ; notranslate">SELECT JSON_REMOVE(@j, '$&#x5B;2]', '$&#x5B;1].b&#x5B;1]', '$&#x5B;1].b&#x5B;1]');
+---------------------------------------------------+
| JSON_REMOVE(@j, '$&#x5B;2]', '$&#x5B;1].b&#x5B;1]', '$&#x5B;1].b&#x5B;1]') |
+---------------------------------------------------+
| &#x5B;&quot;a&quot;, {&quot;b&quot;: &#x5B;true]}]                              |
+---------------------------------------------------+</pre>
<p>The post <a href="https://hoangthuc.com/du-lieu-json-trong-mysql/">Dữ liệu JSON trong MySQL</a> appeared first on <a href="https://hoangthuc.com">Hi You, I am Tade</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://hoangthuc.com/du-lieu-json-trong-mysql/feed/</wfw:commentRss>
			<slash:comments>48</slash:comments>
		
		
			</item>
	</channel>
</rss>
