使用优先级对 MySQL 搜索结果进行排序 [英] Sorting a MySQL search result with Priority

查看:69
本文介绍了使用优先级对 MySQL 搜索结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在这里已经被问过几次了,但是浏览这些线程并没有给我任何帮助.我使用 ORDER BY "column Name" 按字母顺序对搜索结果进行排序.但是,我希望有一个搜索结果总是在最前面.

I know that this question has been asked a few times on here, but looking through those threads yielded me no help. I sort my search results alphabetically using the ORDER BY "column Name". But, I want to have one search result always on top.

例如,我将按名称列对它们进行排序,但我始终希望 Zaboomafoo 成为第一个结果.我该怎么做呢?这是我的 PHP:

So I'll sort them all by their name column for example, but I always want Zaboomafoo to be the first result. How would I go about doing this? Here's my PHP:

$sql = "SELECT * FROM `" . $db_table . "` WHERE (UPPER(city) LIKE UPPER('%$searchq%') OR UPPER(postal) LIKE UPPER('%$searchq%')) AND disable = 1 ORDER BY name";

while($row1 = mysqli_fetch_array($query))
        {
            $name = $row1['name'];
            $business  =$row1['business'];
            $description = $row1['description'];
            $email = $row1['email'];
            $phone = $row1['phone'];
            $image = $row1['image'];
        }

然后我将该信息输出到一个名为 output 的变量中,并将其显示到屏幕上.

And then I ouput that information into a variable called output, and echo it to the screen.

推荐答案

试试这个:

SELECT *
FROM table_name
WHERE (UPPER(city) LIKE UPPER('%$searchq%') 
  OR UPPER(postal) LIKE UPPER('%$searchq%')) 
  AND disable = 1 
ORDER BY IF(name = 'Zaboomafoo', 0, 1), name

这篇关于使用优先级对 MySQL 搜索结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆