SQL在in中傳入引數型別問題

2022-03-12 06:43:06 字數 1982 閱讀 7268

原文:

在orcal或者mysql中,有in的限制條件,in中可以寫數字或者字串,這裡說的是使用mybatis傳入引數問題

例如:如果表中id為字串型別,則in中的id要用單引號引住。

select

*from

table1 t

where t.id in ('

1','

2')

如果表中id為int型別,則in中可以加單引號可以不加

select

*from

table1 t

where t.id in ('

1','2'

)select

*from

table1 t

where t.id in (1,2)

所以如果id為int型別,前台可以直接傳入字串即可,例如傳入 '1,2,3,4,5'的字串,可直接這樣寫

select

*from

table1 t

where t.id in ($)

但id為varchar型別這樣寫就不行了,需要使用regexp_substr函式

select

*from

table1 t1

where t1.id in

(select regexp_substr('

1,2,3

','[^,]+

', 1, level) from

dual

connect

by regexp_substr('

1,2,3

', '

[^,]+

', 1, level) is

notnull

)

這個函式會將'1,2,3'變成 '1','2','3'的單引號形式

傳入引數

select

*from

table1 t1

where t1.id in

(select regexp_substr('

$','

[^,]+

', 1, level) from

dual

connect

by regexp_substr('

$', '

[^,]+

', 1, level) is

notnull

)

自動變為:

select

*from

table1 t

where t.id in ('

1','

2','

3')

總結:無論int型別還是varchar型別,當前臺傳入引數為逗號分隔的字串時,最好都用regexp_substr函式處理一下

in中寫入:

select regexp_substr('

$','

[^,]+

', 1, level) from

dual

connect

by regexp_substr('

$', '

[^,]+

', 1, level) is

notnull

注意,以上方法只適用於oracle,mysql中沒有這種方法,下面說mysql中的解決方法:

使用 find_in_set(id, '1,2,3,4') 函式,

也就是說將

select

*from

table

where id in

'1,2,3

'

改為

select

*from

table

where find_in_set(id, '

1,2,3

')

即可

mybatis中傳入String型別引數的問題

1.出現的問題 需求是想寫乙個按公司名字查詢公司列表的功能,最開始的 如下 dao層介面如下 mybatisdao public inte ce officedao extends treedao1234 mybatis的xml select id,name from sys office wher...

SQL 儲存過程 傳入陣列引數

今天在做統計資料的時候,傳入陣列導致資料不顯示。解決方式和大家分享一下 引數 companyname 北京,天津,上海 declare pointerprev int declare pointercurr int declare tname nvarchar 100 set pointerprev...

jpython 傳入引數 python中引數傳遞

在程式語言中,函式的引數傳遞有兩種情況 按值型別傳遞 num 10 def double arg arg arg 2 print arg double num 呼叫該函式,傳入乙個變數,其實傳入的是該變數的乙個副本,該變數在函式中發生變化,不影響函式外面該變數。按引用型別 位址 傳遞 def cha...