EcStart PHP 技術討論論壇's Archiver

乖小孩 發表於 2009-10-23 15:31

可否達到模糊篩選的效果

我有一個欄位值如下:
[color=Blue]111[/color]
[color=DarkGreen]*22[/color]
[color=Red]***[/color]
[color=Sienna]12*[/color]
如果輸入SELECT的條件111則將[color=Blue]111[/color]及[color=Red]***[/color]篩選出,如果輸入條件為122則將[color=DarkGreen]*22[/color]、[color=Red]***[/color]、[color=Sienna]12*[/color]篩選出來[color=Purple](*代表排除篩選條件)[/color]
請問是否能利用WHERE的命令達到上述的效果?

乖小孩 發表於 2009-10-26 12:33

自己已經測試出來,感謝各位
$str="select serial,a,b,c,d,e,f from $sport_db where
substr(a,1,3)='***'
or  substr(a,1,3) = substr($user_class_cook2,1,3)
or (substr(a,2,2)='**'  and substr(a,1,1) = substr($user_class_cook2,1,1))
or (substr(a,1,1)='*'  and substr(a,2,1) = substr($user_class_cook2,2,1) and substr(a,3,3)='*')
or (substr(a,3,1)='*'  and substr(a,1,2) = substr($user_class_cook2,1,2))
or (substr(a,1,2)='**'  and substr(a,3,1) = substr($user_class_cook2,3,1))
or (substr(a,1,1)='*'  and substr(a,2,2) = substr($user_class_cook2,2,2))";

norman 發表於 2009-10-26 16:52

這樣的寫法很死, 如果邏輯或來源資料做改變, 一定又會大改.

可以先用 PHP 把輸入條件做處理, 之後用 REGEXP 函數比對,
例如輸入 '111' 就要變成 '(1|\\*)(1|\\*)(1|\\*)'
'122' 就變成 '(1|\\*)(2|\\*)(2|\\*)'
'*11' 就變成 '(\\*|\\*)(1|\\*)(1|\\*)'

之後用類似這樣的寫法, 就可以找出你要的資料了....
$str="select serial,a,b,c,d,e,f from $sport_db where a REGEXP '(1|\\*)(1|\\*)(1|\\*)'"

乖小孩 發表於 2009-10-28 14:46

[b] [url=http://bbs.ecstart.com/redirect.php?goto=findpost&pid=98301&ptid=35763]3#[/url] [i]norman[/i] [/b]
感謝norman 您的回覆,我會試試看,謝謝

頁: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.