sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo buscar varias columnas con prioridad en MySQL?

Podría ser una de las soluciones:

SELECT id, desc, abbreviation, etc FROM
( 
   SELECT 1 AS rnk,  id, title as 'desc' , abbreviation , etc FROM shows 
   WHERE abbreviation LIKE '%".$term."%'
   UNION 
   SELECT 2 AS rnk,  id, title as 'desc' , abbreviation , etc FROM shows 
   WHERE title LIKE '%".$term."%' 
) tab
ORDER BY rnk
LIMIT 10;

O puede lograr lo mismo de la siguiente manera:

SELECT  id, desc, abbreviation, etc FROM
(
    SELECT CASE WHEN abbreviation LIKE '%".$term."%' THEN 1 ELSE 2 END As rnk,
    id, title AS desc, abbreviation, etc 
    FROM shows 
    WHERE abbreviation LIKE '%".$term."%' 
    OR title LIKE '%".$term."%' 
) tab
 ORDER BY rnk
 LIMIT 10;