今天做了一个模糊搜索,大概就是一个输入框中输入一个信息,给出相应的结果。这个输入的可能是username,phone,address等等。
看到这个需求,信心满满,咔咔的三两分钟搞定,写的大概是如下:
SELECT `username`, `contacts_name`, `phone`, `store_id`, `role`, `status`, `store_name`, `gps_customer`.`id` FROM `gps_customer` LEFT JOIN `gps_store` ON `gps_customer`.`store_id` = `gps_store`.`id` WHERE (`username` LIKE '%17912345678%') AND (`phone` LIKE '%17912345678%') AND (`store_id`=1) ORDER BY `gps_customer`.`id` DESC
写完commit后,一会测试过来说:不对啊,你这模糊搜索搜索不对啊,我说怎么可能,你这八成是想趁机该需求吧。我自己测试了一下。貌似真的搜索不出来,真的糗,立马改。
我记得mysql有个concat函数可以将字段都合并放在一起。这时试一下。
SELECT `username`, `contacts_name`, `phone`, `store_id`, `role`, `status`, `store_name`, `gps_customer`.`id` FROM `gps_customer` LEFT JOIN `gps_store` ON `gps_customer`.`store_id` = `gps_store`.`id` WHERE (`store_id`=1) AND (concat(`contacts_name`, `phone`, `username`) LIKE '%17912345678%') ORDER BY `gps_customer`.`id` DESC
真的可以。好的,记住这个教训。