mysql where in 字符串分割無法查詢數據換種思路FIND_IN_SET


select barType from bar_info where orderId='H5201702154317';

用字典表聯查后使用group_concat將分組結果連接,得到期望結果:

select group_concat(dtValue) from dictionary where dtType='bar_type' and dtKey in (1,2,3,4,5,6);

但是,當代入子查詢時,結果是NULL,無法得到預期結果:

select group_concat(dtValue) from dictionary where dtType='bar_type' and dtKey in (select barType from bar_info where orderId='H5201702154317') ;

這是因為 where in + 字符串,代入的是'1,2,3,4,5,6',並不是 '1','2','3','4','5','6',再嘗試按拼接參數格式的思路去走:

使用concat(",",barType,","),再replace(),像這樣:concat(",",barType,","):

select replace(concat("'",barType,"'"),",","','") from bar_info where orderId='H5201702154317';

聯合字典表查詢:

select group_concat(dtValue) ,barType from dictionary ,bar_info where dtType='bar_type' and dtKey in (replace(concat("'",barType,","),",","','")) and orderId='H5201702154317';

說明按拼接參數格式的思路也行不通。

這個時候FIND_IN_SET(str,strlist)函數可以出場了。

參數str 要查詢的字符串

參數strlist 字段名 參數以”,”分隔 如 (1,2,6,8)

查詢字段(strlist)中包含(str)的結果,返回結果記錄就是預期。

select group_concat(dtValue) ,barType from dictionary , bar_info  where dtType='bar_type' and FIND_IN_SET(dtKey,barType) and orderId='H5201702154317';

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM