【问题标题】:Order by the values based on search values根据搜索值按值排序
【发布时间】:2016-05-05 09:36:27
【问题描述】:

我有一个包含 3 列的表格

id | name                   | description
------------------------------------------
 1 | Service marketing      |   Description for the marketing
 2 | Marketing              |   Service marketing
 3 | Great customer Service |   Helpful  shows your customers that you really do care about them.

我想从列名、描述中搜索基于“服务”的值,并且我需要按名称和描述按 asc 顺序对值进行排序。我期望结果集如下。

id | name                   | description
------------------------------------------
 1 | Service marketing      |   Description for the marketing
 3 | Great customer Service |   Helpful  shows your customers that you really do care about them.
 2 | Marketing              |   Service marketing

我尝试了以下查询,但不起作用

SELECT * FROM search  where `name` LIKE '%service%'
OR `description` LIKE '%service%'
ORDER BY name,description;

上述查询的结果

id | name                   | description
------------------------------------------
3  |Great customer Service  |   Helpful  shows your customers that you really do c...
2  |Marketing               |   Service marketing
1  |Service marketing       |   Description for the marketing

帮我解决这个问题。谢谢

【问题讨论】:

  • 你想按照word service在两列中的位置排序吗?
  • 排序标准是什么?从您的示例看来,您需要先按“描述”排序,然后按“名称”排序,但我不确定这是否是您想要的
  • 我正在使用 LIKE 搜索基于服务的值。在名称列 ID 1,3 中包含价值服务,所以首先我需要在描述后按名称排序

标签: php mysql


【解决方案1】:

如果你想在列中按“服务”的位置排序,你应该使用这个

select position('Service' IN name) as p1, name, position('Service' IN  description) as p2, description 
from my table 
where  position('Service' IN name) != 0 
and position('Service' IN  description) != 0
order by p1 desc, p2 desc;

【讨论】:

  • #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以使用 near 'name) as p1,name, position('Service' , description) as p2,description from se' 在第 1 行
  • 谢谢。我会检查的。
  • 如果 p1 和 p2 为零。我不想得到这个值。如何在该查询中添加条件?
  • 我已经用 p1 和 p2 = 0 的 where 条件更新了答案
【解决方案2】:

你可以使用类似的东西

SELECT S.*, 
case 
WHEN position('service', name) = 0 THEN 65535
ELSE position('service', name) 
END as p1,
case 
WHEN position('service', description) = 0 THEN 65535
ELSE position('service', description) 
END as p2
FROM search as S where 
name LIKE '%service%' OR description LIKE '%service%'
ORDER BY p1, p2

因为你需要 ASC 订单

【讨论】:

  • 我收到这样的错误您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'name) = 0 THEN 65535 ELSE position('service', name) END as p1, case WHEN posi' at line 3 附近使用的正确语法
【解决方案3】:

嘿,您可以使用以下查询来获得您的预期结果

select position('Service' in name) as p1, name,position('Service' in    description) as p2,description from dd order by p2,name desc

你的预期结果

id | name                   | description
------------------------------------------
1 | Service marketing      |   Description for the marketing
3 | Great customer Service |   Helpful  shows your customers that you really   do care about them.
2 | Marketing              |   Service marketing

【讨论】:

    猜你喜欢
    • 2017-05-07
    • 2018-09-27
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多