对于输入的子记录SQL查询 [英] SQL query for records that are substrings of input

查看:106
本文介绍了对于输入的子记录SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个用户表中有一栏叫名字它具有以下三个行。

Consider a users table that has a column called name which has the following three rows.

  • 在爱丽丝
  • 鲍勃·
  • 卡尔

我想构建一个返回记录中的名字是输入的子查询。举例来说,如果我查询艾丽斯·杰克逊,我想翘。如果我搜索鲍比,我想鲍勃。但是,如果我搜索车,我想没有匹配。排序倒 LIKE

I would like to construct a query that returns records in which the name is a substring of the input. For example, if I query for Alice Jackson, I want Alice. If I search for Bobby, I want Bob. But if I search for Car, I want no matches. Sort of a reversed LIKE.

可以这样做?而能不能做到使用ActiveRecord的语法?

Can this be done? And can it be done using ActiveRecord syntax?

推荐答案

您可以这样做(的 SQL Server语法的):

You could do something like (SQL Server syntax):

SELECT name
FROM users
WHERE 'your query' + ' ' LIKE name + ' %';

下面将找到爱丽丝:

The following will find Alice:

SELECT name
FROM users
WHERE 'Alice Jackson' + ' ' LIKE name + ' %';

SELECT name
FROM users
WHERE 'Alice' + ' ' LIKE name + ' %';

下面将找不到爱丽丝:

The following will not find Alice:

 SELECT name
 FROM users
 WHERE 'Ali' + ' ' LIKE name + ' %';

这篇关于对于输入的子记录SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆