Pergunta

I have a query with a few cases in:

$search = $_GET['search'];


$array = $pdo->prepare("SELECT * , CASE 
     WHEN CHAR_LENGTH(index.product_description) > 32 THEN CONCAT(SUBSTRING(index.product_description, 1, 32), '...') ELSE index.product_description
   END AS product_description, `product_title`, CASE 
     WHEN CHAR_LENGTH(index.product_title) > 18 THEN CONCAT(SUBSTRING(index.product_title, 1, 18), '...')
     ELSE index.product_title
   END AS product_title from `index` where MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) and `category_name` = 'category1' LIMIT :start, :limit");
$array->execute(array(':search' => $search, ':start' => $start, ':limit' => $limit));

I want to add order by relevance, I have this working separately without using cases:

$search = $_GET['search'];
$search2 = $_GET['search'];


$array = $pdo->prepare("SELECT * , MATCH (`product_title`) AGAINST (:search2 IN BOOLEAN MODE) AS relevance 
FROM `index` where MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) and `category_name` = 'category1' order by relevance DESC LIMIT :start, :limit");
$array->execute(array(':search' => $search, ':search2' => $search2, ':start' => $start, ':limit' => $limit));

I have tried adding the:

SELECT * , MATCH (`product_title`) AGAINST (:search2 IN BOOLEAN MODE) AS relevance 
FROM `index`

before and after the cases and it doesnt seem to work.

Where in the first query would I add the order by relevance or how should it be structured when using cases?

Thanks

Foi útil?

Solução

You need an order by clause to order by relevance. You can put the MATCH statement directly in the order by:

SELECT index.*,
       (CASE WHEN CHAR_LENGTH(index.product_description) > 32
             THEN CONCAT(SUBSTRING(index.product_description, 1, 32), '...')
             ELSE index.product_description
        END) AS product_description, `product_title`,
       (CASE WHEN CHAR_LENGTH(index.product_title) > 18
             THEN CONCAT(SUBSTRING(index.product_title, 1, 18), '...')
             ELSE index.product_title
        END) AS product_title
from `index`
where MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) and `category_name` = 'category1'
order by MATCH (`product_title`) AGAINST (:search IN BOOLEAN MODE) desc
LIMIT :start, :limit;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top