dongnao9525 2015-07-19 07:00
浏览 15
已采纳

基于SQL查询订购结果

I currently query a table for items with a certain category. I have extended this query to also looks within the item description column too. My question is, how do i order results so that the category matches are first and the description is last?

`SELECT * FROM myTable WHERE `category` LIKE '%keyword%' || `description` LIKE '%keyword%'

In my above query simply adding a ORDER BY category will not suffice. The only way I can think of doing this is two separate queries with two separate result variables.

I'm using php and mysqli

  • 写回答

2条回答 默认 最新

  • douchunsui2395 2015-07-19 07:24
    关注

    It could be done like this

    SELECT *
    FROM myTable
    WHERE `category` LIKE '%keyword%' || `description` LIKE '%keyword%'
    ORDER BY
        CASE
          WHEN `category` LIKE '%keyword%' THEN 1
          WHEN `description` LIKE '%keyword%' THEN 0
    END 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分