volatileboy Posted December 4, 2009 Share Posted December 4, 2009 Hey people just a small amount of help, im not getting errors but im really stuck with something, MYSQL version is: MySQL client version: 5.0.51a Basically I have a column in my database called article_title, I have an SQL query (below) that searches for different types of match to the input string as you can see. The problem is that if I was to search for "ta" for example, then "stain" would be returned as a match before "tarzan". Here is the query: "SELECT * FROM info_articles WHERE article_title LIKE 'term' OR article_title LIKE '%term' OR article_title LIKE 'term%' OR article_title LIKE '%term%'" Is there any way of searching it so that the results would be organised in a better way? I have looked at various SQL websites for a way around it but I couldn't find anything. Any help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/183987-multi-level-searching/ Share on other sites More sharing options...
fenway Posted December 4, 2009 Share Posted December 4, 2009 If you'd like to rank partial substring matches by distances from string position 0, that's not hard to do in application code. Quote Link to comment https://forums.phpfreaks.com/topic/183987-multi-level-searching/#findComment-971325 Share on other sites More sharing options...
roopurt18 Posted December 5, 2009 Share Posted December 5, 2009 If you want the matches from '$term%' to appear before those matched by '%$term%' there are a couple of ways you can do it. SELECT * FROM info_articles WHERE article_title LIKE 'term' OR article_title LIKE '%term' OR article_title LIKE 'term%' OR article_title LIKE '%term%' could become SELECT *, 0 as `sort_order` FROM info_articles WHERE article_title LIKE 'term' UNION SELECT *, 1 as `sort_order` FROM info_articles WHERE article_title LIKE 'term%' UNION SELECT *, 2 as `sort_order` FROM info_articles WHERE article_title LIKE '%term%' UNION SELECT *, 4 as `sort_order` FROM info_articles WHERE article_title LIKE '%term' ORDER BY `sort_order`, `article_title` If you don't want to use a UNION, then you might get away with some form of a CASE...WHEN in the ORDER BY clause of your original query. Quote Link to comment https://forums.phpfreaks.com/topic/183987-multi-level-searching/#findComment-971693 Share on other sites More sharing options...
fenway Posted December 5, 2009 Share Posted December 5, 2009 Of course, %term% covers all 4 cases. Quote Link to comment https://forums.phpfreaks.com/topic/183987-multi-level-searching/#findComment-971903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.