简体   繁体   中英

PHP mySQL search for concatenated information across 2 fields

I have a functioning mySQL search system.

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

I want to also search if the entered term (including space) would be a concatenation of first_name ' ' surname. How is this done.

I think you will need 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");

Which you may as well reduce down to:

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

Consider making this a full_name field in it's own right, then you can index it for speed reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM