SQL parameters passed in the types of problems in

Original: https://blog.csdn.net/lianzhang861/article/details/82657266 

 

In orcal or mysql, there are restrictions in, in numeric or string can write here that the problem is to use a parameter passed mybatis

For example: If the type is a string in the table id, the id of the use in single quote marks.

SELECT *
FROM TABLE1 t
WHERE t.id in ('1','2')

If the table type int id, it may be in single quotes may not be added

SELECT *
FROM TABLE1 t
WHERE t.id in ('1','2')
SELECT *
FROM TABLE1 t
WHERE t.id in (1,2)

If it is of type int id, string directly into the reception can be, for example, '1,2,3,4,5' string passed directly write

SELECT *
FROM TABLE1 t
WHERE t.id in (${ids})

But id varchar type is written to die, you need to use the function 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 NOT NULL

)

This function will ', 2,3' into the form of single quotation marks '1', '2', '3'

Incoming parameters

SELECT *
FROM table1 t1 
WHERE t1.id in (

SELECT REGEXP_SUBSTR('${ids}','[^,]+', 1, LEVEL) FROM DUAL

CONNECT BY REGEXP_SUBSTR('${ids}', '[^,]+', 1, LEVEL) IS NOT NULL

)

Automatically become:

SELECT *
FROM TABLE1 t
WHERE t.id in ('1','2','3')

Summary:
Whether an int or varchar type, current stage its argument is a comma-separated string, it is best to handle it all function with REGEXP_SUBSTR

in written:

SELECT REGEXP_SUBSTR('${ids}','[^,]+', 1, LEVEL) FROM DUAL

CONNECT BY REGEXP_SUBSTR('${ids}', '[^,]+', 1, LEVEL) IS NOT NULL

Note that the above method is only applicable to the oracle, mysql no such method, said the following solutions in mysql:

Use  FIND_IN_SET (id, '1,2,3,4') function,

That will

select * from table where id in '1,2,3' 

Changed

 select * from table where FIND_IN_SET(id, '1,2,3')

To


Guess you like

Origin www.cnblogs.com/shiysin/p/11276126.html