查询搜索字母到字母?

发布于 2024-11-29 21:04:03 字数 1601 浏览 0 评论 0原文

我正在进行搜索,并根据在搜索框中输入的字母提供建议。

示例:

我在数据库中有这些词:salam、salavat、sabos、sandal、sefid、 siah 和 ...
我在搜索框中输入:s --显示此 话--> salam、salavat、sabos、sandal、sefid、siah
现在如果 输入:sa --显示此词--> 萨拉姆,萨拉瓦特,萨博斯, sandal,
如果输入:sala --显示此词-->萨拉姆, salavat
如果输入:salam --显示此词--> 萨拉姆

我正在使用 jQuery 事件 .keyup()(keyup 工作正常)并在后端使用 codeigniter(在 codeigniter 中查询不是我想要的)。

查看我的查询(在此查询中,$find 包含已输入的序列,并且我使用 like 关键字在数据库中搜索):

$find = $this->input->post('find');
    $where  =   "date LIKE '$find' OR
            name LIKE '$find' OR 
            star LIKE '$find' OR
            address LIKE '$find' OR
            number_phone LIKE '$find' OR 
            type LIKE '$find' OR
            service LIKE '$find' OR
            units LIKE '$find' OR
            useradmin LIKE '$find'";

$query = $data['results'] = $this->db->query("SELECT @rownum:=@rownum+1 rownum, t.*
        FROM (
        SELECT *
        FROM my_table
        WHERE 
            $where
        ORDER BY id desc
        LIMIT $offset, $coun_page       
        ) t,
        (SELECT @rownum:=0) r");

以下查询有效,但是我无法使用它,因为 rownum

$this->db->order_by("id", "desc")->like('name', $name)->get('my_table')

我如何获得有关 rownum 的建议?

I am making a search with suggestions offered on basis of letters typed in the search box.

Example:

I have in database this words : salam, salavat, sabos, sandal, sefid,
siah and ...

I typed in search box : s --display this
words
--> salam, salavat, sabos, sandal, sefid, siah
Now if
typed : sa --display this words--> salam, salavat, sabos,
sandal,

If typed : sala --display this words--> salam,
salavat

If typed : salam --display this words-->
salam

I am using jQuery event .keyup()(keyup is working fine) and using codeigniter at the backend(query in codeigniter not what that i want).

See my query(In this query, $find contains the sequence already typed in, and i am using like Keyword for searching in the database):

$find = $this->input->post('find');
    $where  =   "date LIKE '$find' OR
            name LIKE '$find' OR 
            star LIKE '$find' OR
            address LIKE '$find' OR
            number_phone LIKE '$find' OR 
            type LIKE '$find' OR
            service LIKE '$find' OR
            units LIKE '$find' OR
            useradmin LIKE '$find'";

$query = $data['results'] = $this->db->query("SELECT @rownum:=@rownum+1 rownum, t.*
        FROM (
        SELECT *
        FROM my_table
        WHERE 
            $where
        ORDER BY id desc
        LIMIT $offset, $coun_page       
        ) t,
        (SELECT @rownum:=0) r");

The following query works, but i can not use of it because of rownum:

$this->db->order_by("id", "desc")->like('name', $name)->get('my_table')

How do I get suggestions with rownum?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2024-12-06 21:04:03

要将子字符串与 LIKE 查询匹配,您需要使用通配符,在 SQL 中是百分比字符 %

例如,

WHERE foo LIKE '%bar%' 将查找 foo 列包含子字符串 'bar' 的所有行。

WHERE foo LIKE 'bar%' 将查找 foo 列以子字符串 'bar' 开头的所有行。

WHERE foo LIKE '%bar' 将查找 foo 列以子字符串 'bar' 结尾的所有行。

附言。在反引号中包含列名称始终是一个好主意,以避免与(可能的)保留名称发生冲突,例如 datetype

SELECT `column1`, `column2` FROM `table` WHERE `column3` = 'value'

For matching a substring with a LIKE query, you need to use a wildcard character, which in SQL is the percentage-character %.

E.g.

WHERE foo LIKE '%bar%' would find all rows where the column foo contains the substring 'bar'.

WHERE foo LIKE 'bar%' would find all rows where the column foo begins with the substring 'bar'.

WHERE foo LIKE '%bar' would find all rows where the column foo ends with the substring 'bar'.

PS. it's always a good idea to contain column names in back ticks to avoid collisions with (potentially) reserved names, such as date and type.

SELECT `column1`, `column2` FROM `table` WHERE `column3` = 'value'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文