我正在开发使用PHP和Codeigniter构建的API,并将MySQL用作数据库。
I am developing an API built with PHP and Codeigniter and I use MySQL as database.
我有一个名为Users的用户表,其中包含以下几列:
I have a user table called Users which contains the following columns:
- ID - Firstname - Lastname - Email用户如何使用全名搜索此表?例如,按Martin或Smith进行搜索都可以,但不能按Martin Smith进行搜索。
How can a user search this table using a full name? For example it works fine to search either by Martin or Smith, but not Martin Smith.
这是我现在正在使用的查询:
This is the query I am using now:
$this->db->select('*'); $this->db->from('users'); $this->db->like('firstname', $args['query']); $this->db->or_like('lastname', $args['query']);更新
如果按照以下建议使用此查询,我什么也找不到。不能使用全名,名字或姓氏。然后,名字和姓氏都必须是相同的单词。
If I use this query as suggested below I cannot find anything. Not with full name, first name or last name. Then both first name AND last name needs to be the same word.
$this->db->select('*'); $this->db->from('users'); $this->db->like('firstname', $args['query']); $this->db->like('lastname', $args['query']);推荐答案
直奔我的脑袋,但是那怎么办
Straight out of my head, but what about
$input = 'Martin Smith'; $names = explode($input); $this->db->select('*'); $this->db->from('users'); $this->db->where_in('firstname', $names); $this->db->or_where_in('lastname', $names);应输出以下内容:
SELECT * FROM users WHERE firstname IN ('Martin', 'Smith') OR lastname IN ('Martin', 'Smith')更多推荐
搜索全名,而不只是名字或姓氏
发布评论