SQL (Oracle) 中的动态“LIKE”语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8327616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:28:31  来源:igfitidea点击:

Dynamic 'LIKE' Statement in SQL (Oracle)

sqloracle

提问by marius_neo

I am trying to select from a table with the following structure :

我正在尝试从具有以下结构的表中进行选择:

MATERIALS 
id
shortname 
longname

all the lines where the long name is like the short name.

长名称与短名称相同的所有行。

I've tried the solution presented here : Dynamic Like Statement in SQL, but it doesn't work for me.

我已经尝试过这里提供的解决方案:SQL 中的动态 Like 语句,但它对我不起作用。

SELECT * from MATERIALS where longname like (shortname + '%');

doesn't work in Oracle.

在 Oracle 中不起作用。

回答by ypercube??

You can use the CONCAT()function:

您可以使用该CONCAT()功能:

SELECT * 
FROM MATERIALS 
WHERE longname LIKE CONCAT(shortname, '%')

or even better, the standard || (double pipe)operator:

甚至更好的是,标准|| (double pipe)运算符:

SELECT * 
FROM MATERIALS 
WHERE longname LIKE (shortname || '%')


Oracle's CONCAT()function does not take more than 2 arguments so one would use the cumbersome CONCAT(CONCAT(a, b), c)while with the operator it's the simple: a || b || c

Oracle 的CONCAT()函数不接受超过 2 个参数,因此使用繁琐CONCAT(CONCAT(a, b), c)的操作符,而使用运算符则很简单:a || b || c

回答by MatBailie

CONCAT()is probably the most technically correct.

CONCAT()可能是技术上最正确的。

For convenience, however, ORACLE doeshave an equivalent operator to +...

然而,为了方便起见,ORACLE确实有一个等效的运算符来+...

SELECT * 
FROM MATERIALS 
WHERE longname LIKE (shortname || '%')