Suppose I have a news table with id(PK) columns id(PK) , title , description . There is also some search query, for example, 'самолет' . How can I write a query so that I first find all the entries with a match in the title , then everything with a match in the description , excluding those already found, and output everything in that order? Something of a type (SELECT * FROM news WHERE title LIKE '%самолет%') UNION ALL (SELECT * FROM news WHERE description LIKE '%самолет%') , but without a match in the second part of the query.
|
2 answers
SELECT * FROM news WHERE title LIKE '%самолет%' or description LIKE '%самолет%' order by title LIKE '%самолет%' By operator like it is quite possible to sort. (as usual, I could mess up the sort order)
- Rather
order by title LIKE '%самолет%' desc. I always confuse asc and desc - Minor - Yes, it seems like that. - Regent
|
select distinct <перечислить все столбцы кроме sortOrder> from (SELECT 1 AS sortOrder, news.* FROM news WHERE title LIKE '%самолет%' UNION all SELECT 2, news.* FROM news WHERE description LIKE '%самолет%' ) X ORDER BY sortOrder |