简体   繁体   English

SQL Select模式,例如column_name

[英]SQL Select pattern like column_name

Are there such things as a sql query whereby the results are limited with being contained in the query value. 是否存在诸如sql查询之类的问题,从而限制了结果包含在查询值中。

Normally like is : 通常像是:

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

I would like it to be 我希望成为

SELECT column_name(s)
FROM table_name
WHERE pattern LIKE column_name;

For example: 例如:

id   |   val
1    |   c:/tests/one
2    |   c:/tests/two
3    |   c:/

I can query using 'c:/tests/two/fail/results', and be returned 2 and 3, as they are sub strings. 我可以使用'c:/ tests / two / fail / results'进行查询,并返回2和3,因为它们是子字符串。

SELECT column_name
FROM table_name 
WHERE instr('c:/tests/two/fail/results', column_name) > 0;

If you really want to use LIKE then you could use 如果您真的想使用LIKE,则可以使用

SELECT 
    * 
FROM
    example
WHERE
    'c:/tests/two/fail/results'
LIKE
    CONCAT(val, '%');

Please regard that the right side of LIKE is a pattern and you've got to append the wildcard character '%' to it. 请注意,LIKE的右侧是一个模式,您必须在其后附加通配符'%'。

Demo 演示版

Note: 注意:

Of course will the solution of juergen d work and may even be faster. 当然,绝大部分的解决方案都会起作用,甚至可能更快。

Another issue with my solution is that the column val may not contain wild card characters itself. 我的解决方案的另一个问题是,列val本身可能不包含通配符。 Now the two wild card characters for LIKE are 现在,LIKE的两个通配符是

%   Matches any number of characters, even zero characters
_   Matches exactly one character

The underscore is not only an allowed character in a file path, it's often used. 下划线不仅是文件路径中允许的字符,而且经常被使用。 I recommend strongly to use the approach of juergen d. 我强烈建议使用juergen d的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM