2016-12-29 64 views
0

例如,我有一個名爲表JSON列A我該怎麼辦在mysql中包含json的where子句?

的Json列包含這樣的JSON數據:

record 1 : {"dept_code": "012", "unit_code": "22"} 
record 2 : {"dept_code": "013", "unit_code": "23"} 
etc 

我想借此與JSON列包含dept_code = 012

數據記錄
SELECT * FROM table_A WHERE json = ...(looking dept_code = 012)... 

我該怎麼做?

注:

我試圖找到在現場計算器的答案,但我沒有找到它。所以我使這個問題

回答

2

也許這可以幫助:

SELECT * FROM table_A where json like '%"dept_code": "012"%'; 
+0

太棒了,它的工作原理。謝謝 –

0
...WHERE JSON_CONTAINS(`json `, '{"dept_code " : "012"}') 
+0

它不起作用。查詢運行時,沒有記錄顯示 –

0

很肯定JSON_CONTAINS是你想要的。 View the docs here

我沒有沙箱,現在來測試這個權利,但你的榜樣將轉化是這樣的:由於dept_code是存儲在JSON列對象的屬性

select * from table_A 
where json_contains(json, '012', '$.dept_code') 

+0

它不起作用。運行查詢時,存在如下錯誤:'錯誤代碼:3141 參數2中的json_contains函數中的JSON文本無效:「文檔根目錄不能跟隨其他值。」 ' –

+0

@mosestoh try'where json_contains(json,''012'','$ .dept_code')',即將你的012加入引號中,因爲你的json值還有一個字符串。至少解決了同樣錯誤信息的_my_問題。 你接受的那個答案非常活潑。 –