搜索全名,而不只是名字或姓氏

编程入门 行业动态 更新时间:2024-10-26 22:27:51
本文介绍了搜索全名,而不只是名字或姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

搜索全名,而不只是名字或姓氏

本文发布于:2023-10-12 20:59:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   全名   姓氏   名字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!