查询搜索字母到字母?
我正在进行搜索,并根据在搜索框中输入的字母提供建议。
示例:
我在数据库中有这些词:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要将子字符串与
LIKE
查询匹配,您需要使用通配符,在 SQL 中是百分比字符%
。例如,
WHERE foo LIKE '%bar%'
将查找foo
列包含子字符串'bar'
的所有行。WHERE foo LIKE 'bar%'
将查找foo
列以子字符串'bar'
开头的所有行。WHERE foo LIKE '%bar'
将查找foo
列以子字符串'bar'
结尾的所有行。附言。在反引号中包含列名称始终是一个好主意,以避免与(可能的)保留名称发生冲突,例如
date
和type
。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 columnfoo
contains the substring'bar'
.WHERE foo LIKE 'bar%'
would find all rows where the columnfoo
begins with the substring'bar'
.WHERE foo LIKE '%bar'
would find all rows where the columnfoo
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
andtype
.