本文主要介紹了mysql 一個巧用字符函數做數據篩選的題,需要的朋友可以參考下,希望能幫助到大家。
問題描述:
結構:
test 有兩個字段,
分別是col1和col2,都是字符字段,
里面的內容都是用,號分隔的三個數字,并且是一一對應的,
比如col1內容是:26,59,6
col2內容是:1502.5,1690,2276.77
一一對應就是26的值是1502.5,59是1690,6對應2276.77
搜索條件:
選擇一個id,比如選擇59,再輸入一個數字,比如:2000
然后就是搜索col1中存在id=59的記錄,然后搜索col2小于2000,即1690
舉例:
如有以下三條記錄,搜索id為59,值小于2000的記錄:
26,59,6 | 1502.5,1690,2276.77
59,33,6 | 3502.1,1020,2276.77
22,8,59 | 1332.6,2900,1520.77
搜索到這三個記錄存在id為59,之后判斷第二個搜索條件應為(即用對應id位置的數字對比):
16903502.1>2000
1520.77
drop?table?test;? create?table?test?(?col1?varchar(100),col2?varchar(100));? insert?test?select? '26,59,6',?'1502.5,1690,2276.77'?union?all?select? '59,33,6',?'3502.1,1020,2276.77'?union?all?select? '22,8,59',?'1332.6,2900,1520.77';? select?col1,col2? from?(select?*,find_in_set('59',col1)?as?rn?from?test)?k? where?substring_index(concat(',',substring_index(col2,',',rn)),',',-1)? ?<p style="white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); text-align: left; font: 14px/26px Arial; widows: 1; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px">+---------+---------------------+</p><p>| col1 ? ?| col2 ? ? ? ? ? ? ? ?|</p><p>+---------+---------------------+</p><p>| 26,59,6 | 1502.5,1690,2276.77 |</p><p>| 22,8,59 | 1332.6,2900,1520.77 |</p><p>+---------+---------------------+</p><p class="art_xg">相關推薦:</p><p class="art_xg"><a href="http://www.php.cn/php-weizijiaocheng-380908.html" target="_self">php替換字符串中的一些字符函數str_ireplace()</a></p><p class="art_xg"><a href="http://www.php.cn/php-weizijiaocheng-380908.html" target="_self">php替換字符串中的一些字符函數str_ireplace()</a></p><p class="art_xg"><a href="http://www.php.cn/php-weizijiaocheng-380908.html" target="_self">php替換字符串中的一些字符函數str_ireplace()</a></p>
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END