in Education by
I'm writing some very simple search functionality for a list of FAQ's. I'm splitting the search string on a variety of characters, including spaces. Then performing a select along the lines of SELECT * FROM "faq" WHERE ((LOWER("Question") LIKE '%what%' OR LOWER("Question") LIKE '%is%' OR LOWER("Question") LIKE '%a%' OR LOWER("Question") LIKE '%duck%')) I've had to edit this slightly as its generated by our data access layer but it should give you an idea of whats going on. The problem is demonstrated well with the above query in that most questions are likely to have the words a or is in them, however I can not filter these out as acronyms may well be important for the searcher. What has been suggested is we order by the number of matching keywords. However I have been unable to find a way of doing this in SQL (we do not have time to create a simple search engine with an index of keywords etc). Does anyone know if there's a way of counting the number of LIKE matches in an SQL statement and ordering by that so that the questions with the most keywords appear at the top of the results? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I assume the list of matching keywords is being entered by the user and inserted into the query dynamically by the application, immediately prior to executing the query. If so, I suggest amending the query like so: SELECT * FROM "faq" WHERE ((LOWER("Question") LIKE '%what%' OR LOWER("Question") LIKE '%is%' OR LOWER("Question") LIKE '%a%' OR LOWER("Question") LIKE '%duck%')) order by case when LOWER("Question") LIKE '%what%' then 1 else 0 end + case when LOWER("Question") LIKE '%is%' then 1 else 0 end + case when LOWER("Question") LIKE '%a%' then 1 else 0 end + case when LOWER("Question") LIKE '%duck%' then 1 else 0 end descending; This would even enable you to "weight" the importance of each selection term, assuming the user (or an algorithm) could assign a weighting to each term. One caveat: if your query is being constructed dynamically, are you aware of the risk of SQL Insertion attacks?

Related questions

0 votes
    Let's say there is a table structured like this: ID | article_id | article_count | created_at ---| ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    Let's say there is a table structured like this: ID | article_id | article_count | created_at ---| ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    The ORDER BY clause can only be used in (a) SELECT queries (b) INSERT queries (c) GROUP BY ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    The predicate in a where clause can involve Boolean operations such as and. The result of true and ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    I am getting empty row on following sql SELECT * FROM flyers WHERE fId='6' AND 'userId'='400' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I am trying to make a a query against a table and am kind of stuck in constructing the WHERE ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I have this table which stores containers by region and the number of coffee pouches in each of the containers. if object_id( ... = 1 ) as sq1 ) as sq2 where sq2.LagRunningTotal...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Is there any way to convert following SQL statement into LINQ? select ve.EntityID , fin1.FinanceStat as ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which oracle is the join condition is specified using the WHERE clause: (a) Oracle 9i (b) Oracle ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    ANSI-standard SQL allows the use of special operators in conjunction with the WHERE clause. A special ... Schemes topic in chapter Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    5. Write a program to count the number of spaces, upper case and lower case characters in a string, where the ... entered by the user Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
...