Mysql 按列最佳匹配的LIKE查询
我有一个名为customers的表,并运行如下查询Mysql 按列最佳匹配的LIKE查询,mysql,sql-like,Mysql,Sql Like,我有一个名为customers的表,并运行如下查询 SELECT * FROM customers WHERE email LIKE '%ab%' OR first_name LIKE '%ab%'; 结果如下 ------+-------+-------+------- + | email | first_name | +-----+-------+-------+------- + | abc@gmail.com | xyz | | xyz@gmail
SELECT * FROM customers WHERE email LIKE '%ab%' OR first_name LIKE '%ab%';
结果如下
------+-------+-------+------- +
| email | first_name |
+-----+-------+-------+------- +
| abc@gmail.com | xyz |
| xyz@gmail.com | abc |
+-----+-------+-------+--------+
我需要的是,名字是搜索关键字的最佳匹配。因此,我首先需要该行。您可能希望通过以下方式添加订单:
SELECT *
FROM customers
WHERE email LIKE '%ab%' OR first_name LIKE '%ab%'
ORDER BY CASE WHEN first_name LIKE '%ab%' THEN 1 ELSE 2 END;
您可能希望通过以下方式添加订单:
SELECT *
FROM customers
WHERE email LIKE '%ab%' OR first_name LIKE '%ab%'
ORDER BY CASE WHEN first_name LIKE '%ab%' THEN 1 ELSE 2 END;