我在 mysql 数据库中有一张化学品表。我想执行通配符搜索 -
SELECT * FROM chemical WHERE chemical like '%toluene%'
SQL查询结果为-
Bromotoluene
Chlorotoluene (Benzyl chloride)
Cymene (Isopropyltoluene)
Dinitrotoluene (DNT)
Isopropyl toluene (Cymene)
Octachlorotoluene
Toluene (Toluol)*
Toluene di-isocyanide (TDI)
Trinitrotoluene (TNT)
与字符串的第一个字符(在本例中为“toluene”)完全匹配的结果是否可以列在任何其他结果之前?例如 - 我想展示 -
Toluene (Toluol)
Toluene di-isocyanide (TDI)
首先,然后是其余的结果。
非常感谢您的帮助。
最佳答案
我会将查询分成两部分:
select * FROM chemical where chemical like 'toluene%'
union
select * FROM chemical where chemical like '%toluene%'
为了把更相关的结果放在最前面。 union
将自动排除重复的结果。
关于MYSQL 通配符搜索 - 排序结果问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47756745/