mysql - How to set priority for search value -
i've got problem setting priority search value in database. find courses, have phrase (or contain word of phrase) in name course. id addition, course have same name searched phrase in first place in returned data.
records course table:
+----+------+-------------------+ | id | code | name | +----+------+-------------------+ | 1 | jpa | jpa lorem ipsum | | 2 | jsf | jsf lorem ipsum | | 3 | hib | hib lorem ipsum | | 4 | teb | teb lorem ipsum | | 5 | zrn | in advance | +----+------+-------------------+
my query:
select `course`.`id`, `course`.`name` `course` `name` 'teb lorem ipsum' or `name` '%teb%' or `name` '%lorem%' or `name` '%ipsum%'
database returns:
+----+-----------------+ | id | name | +----+-----------------+ | 1 | jpa lorem ipsum | | 2 | jsf lorem ipsum | | 3 | hib lorem ipsum | | 4 | teb lorem ipsum | +----+-----------------+
i receive:
+----+-----------------+ | id | name | +----+-----------------+ | 4 | teb lorem ipsum | | 1 | jpa lorem ipsum | | 2 | jsf lorem ipsum | | 3 | hib lorem ipsum | +----+-----------------+
record, have 4id matching , first place in returned data. question, how can it?
thanks in advance!
this might trick.
select `course`.`id`, `course`.`name` `course` `name` 'teb lorem ipsum' or `name` '%teb%' or `name` '%lorem%' or `name` '%ipsum%' order case when `name`='teb lorem ipsum' 1 else 2 end asc
in short, checks if name equals value , gives 1, else 2. , orders in ascending order.
Comments
Post a Comment