0

我的网站是一种字典。搜索关键字时,我想同时查看“单词”和“含义”列并显示与单词或含义匹配的所有内容。

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

这样就有重复(当关键字匹配单词和含义时)

当我再次联合它们时,顺序将不正确

如何才能做到这一点?我需要不同匹配结果的计数+不同匹配结果本身,但首先显示匹配词,然后显示匹配含义。

4

1 回答 1

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
于 2013-04-25T20:18:13.370 回答