我的网站是一种字典。当搜索关键字时,我想查看“单词”和“含义”列,并显示所有匹配单词或含义的内容。
select count(id) words where word like @keyword or meaning like @keyword
select * from words where word like @keyword or meaning like @keyword order by word
但我想首先显示匹配的单词然后显示匹配的含义。订单不正确
当我把它们分开时:
select count(id) words where word like @keyword
select * from words where word like @keyword order by word
select count(id) words where meaning like @keyword
select * from words where meaning like @keyword order by word
这种方式有重复(当关键字匹配单词和含义时)
当我再次联合他们时,订单将不正确
怎么办呢?我需要不同匹配结果的计数+不同的匹配结果本身,但首先显示匹配的单词,然后显示匹配的含义。
答案 0 :(得分:1)
在ORDER BY子句中使用case语句来区分它与单词匹配的时间:
select *
from words
where word like @keyword or meaning like @keyword
order by
case when word like @keyword then 0 else 1 end
, word