搜索全名,而不只是名字或姓氏 [英] Search with full name instead of just first name or last name

查看:148
本文介绍了搜索全名,而不只是名字或姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用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')

这篇关于搜索全名,而不只是名字或姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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