0

我有一个包含 first_name 和 last_name 的学生表。当我对名字或姓氏进行搜索时,它会为我提供正确的记录,但是当我使用全名搜索时,它不会返回任何内容。这是我的查询

SELECT 
  `student_personal_info`.`spi_id` AS `sid`, 
  `student_personal_info`.`spi_first_name` AS `fname` 
FROM 
  `student_personal_info` 
WHERE 
 ((spi_first_name like '%test developer%') OR (spi_last_name like '%test developer%') 
ORDER BY 
 `spi_first_name` ASC

有什么方法可以通过这两个字段组合进行搜索。

如果我们输入名字.中间名和姓氏,它将给出记录,但如果名字和姓氏不是中间名,则不获取。

SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE (
spi_scholar_no LIKE '%amir hussain khan%'
)
OR (
CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%amir hussain khan%'

我也试过这个,但它只适用于完全匹配。

SELECT `student_parents_info`.`spri_spi_id` AS `sid`
FROM `student_parents_info`
WHERE (MATCH (spri_first_name, spri_middle_name, spri_last_name) AGAINST ('Pramod Kumar Sharma*' IN BOOLEAN MODE ) )
ORDER BY `spri_first_name` ASC
LIMIT 0 , 30

请提出任何其他建议。

4

5 回答 5

1

尝试使用 concat() 搜索..

SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name`    AS `fname`
FROM `student_personal_info`
  WHERE CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%test%'
ORDER BY `spi_first_name` ASC

它总是对我有用。

于 2013-03-13T12:01:37.433 回答
0

像这样试试

SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname` 
FROM `student_personal_info`
WHERE 
    ((spi_first_name like '%amir khan%') OR (spi_last_name like '%test develope%'))
ORDER BY `spi_first_name` ASC

你错过了')'

于 2013-03-13T11:53:53.557 回答
0

您是否收到用于执行搜索的全名?或者你总是得到2个分开的(名字和姓氏)?

如果您得到一个全名字符串,请将您的查询更改为连接名字和姓氏,并使用

GROUP BY HAVING(full_name LIKE %search_term%) 

如果你得到两个单独的字符串,只需在名字和姓氏 LIKE 测试之间将 OR 更改为 AND。

于 2013-03-13T11:54:45.503 回答
0

尝试使用 CONCAT:

SELECT
  `student_personal_info`.`spi_id` AS `sid`,
  `student_personal_info`.`spi_first_name` AS `fname`
FROM
  `student_personal_info`
WHERE 
  CONCAT(spi_first_name, ' ', spi_last_name) LIKE '%test develope%'
ORDER BY
 `spi_first_name` ASC
于 2013-03-13T11:55:33.810 回答
0

拆分您的test developetotestdevelope使用 php,然后编写如下查询:

$first = 'test';
$last  = 'develope';

$query = "SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname` FROM `student_personal_info` WHERE 
(spi_first_name like '%".$first."%') OR (spi_last_name like '%".$last."%') 
ORDER BY `spi_first_name` ASC";

如果您有两个以上的单词,请拆分单词并创建动态where查询。

于 2013-03-13T11:59:03.200 回答