2013-06-18 64 views
-1

我有這個$value= 24153; 我有一個可以容納這樣{"id":"2","value":["3"]}或類似的一個或多個值的字段這個{"id":"2","value":["3","4"]}{"id":"2","value":["3","4","2"]} 我有這樣的正則表達式工作正常,但如果一個值存在的回報。 我需要改善這個正則表達式的情況下,有多個字段中的值。REGEXP BINARY SQL語法問題

REGEXP BINARY \'(.*{"id":"2","value":\["[^\"]*['.$value.'][^\"]*",?)+\]}.*\' 
+0

-1用於在數據庫中存儲JSON,然後使用REGEXP進行搜索。這個唯一的想法讓我感到噁心。 –

回答

-1
$value = 4; 
$subject_a = '{"id":"2","value":["4"]}'; 
$subject_b = '{"id":"2","value":["1","4","1","1"]}'; 
$subject_c = '{"id":"2","value":["1","1","1","1","1","44","1","1"]}'; 
$pattern = '/{"id":"2","value":\[("|\d|,)*"' . $value . '"("|\d|,)*\]/'; 

$matches[0] = preg_match($pattern,$subject_a); 
$matches[1] = preg_match($pattern,$subject_b); 
$matches[2] = preg_match($pattern,$subject_c); 

echo '<pre>'; 
var_dump($matches); 
echo '</pre>'; 

結果將是:

array(3) { 
    [0]=> int(1) 
    [1]=> int(1) 
    [2]=> int(0) 
} 
+0

確定這裏是舊查詢返回結果SELECT ... AND extra_fields REGEXP BINARY'(。* {「id」:「2」,「value」:\ [「[^ \」] * [1] [^ \ 「] *」,?)+ \]}。*'ORDER BY i.id DESC,但僅基於$ subject_a結構。這是不返回任何內容的新查詢! SELECT ... AND extra_fields REGEXP BINARY'{「id」:「2」,「value」:\ [(「| \ d |,)*」1「(」| \ d |,)* \]'ORDER BY i.id DESC – shai

+0

SELECT * FROM表WHERE列REGEXP BINARY'{「id」:「2」,「value」:\ [[0-9「,] *」4「[0-9」,] * \] }';假設4是你的價值 –

0

這裏是一個正則表達式會做你想要的東西 - 但我有你的常識同意 - 存儲JSON,然後用正則表達式來從中提取數據令人噁心

REGEXP BINARY \'(.*{"id":"2","value":\[("[^\"]*",)*"'.$value.'"(,"[^\"]*")*)+\]}.*\'