PHP mySQL搜索2个字段的连接信息

时间:2012-06-27 10:01:25

标签: php mysql concat

我有一个正常运行的mySQL搜索系统。

mysql_query("SELECT * FROM electors WHERE constituency = '$constituency' AND ward = '$ward' AND (surname LIKE '$q' OR first_name LIKE '$q') LIMIT 18");

我还要搜索输入的术语(包括空格)是否为first_name ' ' surname.这是如何完成的连接。

1 个答案:

答案 0 :(得分:0)

我认为你需要CONCAT

mysql_query("SELECT * FROM electors WHERE constituency = '$constituency' AND ward = '$ward' AND (surname LIKE '$q' OR first_name LIKE '$q' OR CONCAT(first_name,' ',surname) LIKE '$q') LIMIT 18");

你可以减少到:

mysql_query("SELECT * FROM electors WHERE constituency = '$constituency' AND ward = '$ward' AND CONCAT(first_name,' ',surname) LIKE '%$q%' LIMIT 18");

考虑将它作为一个full_name字段,然后你可以出于速度原因对其进行索引。