学生的名字,中间名,姓氏在学生个人信息的不同领域。我需要通过全名执行搜索操作

时间:2015-09-05 13:10:13

标签: php mysql

select a.studentid, CONCAT(a.firstname,' ',a.middlename,' ',a.lastname) AS studentname, a.birthdate,b.admissiondate,b.status,c.facultyname,d.classname,e.sectionname
from studentpersonalinfo as a, studentacademicinfo as b, faculty as c, class as d, section as e 
WHERE a.studentid=b.studentid AND b.facultyid=c.facultyid AND b.classid=d.classid AND b.sectionid=e.sectionid AND b.status='running' and studentname LIKE '%$name%'  ORDER BY $sort $order limit $offset,$rows

1 个答案:

答案 0 :(得分:1)

我已将WHERE Condition替换为studentname LIKE '%$name%' CONCAT_WS(' ', a.firstname,a.middlename,a.lastname) LIKE '%$name%'

CONCAT_WS的一般语法是:

CONCAT_WS (separator, string1, string2,…)

现在它应该有效:

SELECT a.studentid, CONCAT(a.firstname,' ',a.middlename,' ',a.lastname) AS studentname, a.birthdate, b.admissiondate, b.status, c.facultyname, d.classname, e.sectionname 
FROM studentpersonalinfo as a, studentacademicinfo as b, faculty as c, class as d, section as e 
WHERE a.studentid = b.studentid AND b.facultyid = c.facultyid AND b.classid = d.classid AND b.sectionid = e.sectionid AND b.status = 'running' and CONCAT_WS(' ', a.firstname, a.middlename, a.lastname) LIKE '%$name%' ORDER BY $sort $order limit $offset, $rows

它也可以像你一样使用它,只需将CONCATSELECT替换为CONCAT_WS(' ', a.firstname, a.middlename, a.lastname) AS studentname

所以一般来说你的代码应该是:

    SELECT a.studentid, CONCAT_WS(' ', a.firstname, a.middlename, a.lastname) AS studentname, a.birthdate, b.admissiondate, b.status, c.facultyname, d.classname, e.sectionname 
    FROM studentpersonalinfo as a, studentacademicinfo as b, faculty as c, class as d, section as e 
    WHERE a.studentid = b.studentid AND b.facultyid = c.facultyid AND b.classid = d.classid AND b.sectionid = e.sectionid AND b.status = 'running' and studentname LIKE '%$name%' ORDER BY $sort $order limit $offset, $rows

现在它应该正常工作......

有关详细信息,请参阅:https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws