0

I have the following structure of a table [id - title - tag - ..] I want to achieve the following:

If there is a record in table with title "I love my job and it is my hobby" If a query is submitted having two words from the sentence then this sentence should be selected. E.g. query "love hobby". It should give me the above title and not for example "I love my job". At least the sentence with more words matching the query keywords first then the less ones later.

how can I do this search on the title column of my table?

I apologize if explanation not clear...more than happy to help clarify.

Thank you all

4 Answers 4

2

Try this :

SELECT title FROM your_table WHERE title LIKE '%love%' AND title LIKE '%hobby%'

Sign up to request clarification or add additional context in comments.

4 Comments

well the keywords I gave are an example...so am assuming I will replace them with $keyword1 and $keyword2....but will this achieve what I really need i.e. identifying the one with both keywords present before the title with one keyword present?
only values in title with both love and hobby will be shown.
aha and what about if i wish to include the titles with one of them only? I want to display them after the ones matching both keywords. btw what if I don't know the number of keywords inserted?
sorry should clarify that search query is all in one keyword...so should I separate them first?
1

Look into mysql's built in full text search capabilities. In boolean mode, you could transform your query to +love +hobby and have results returned without full table scans. Be aware that this only works with myisam tables, might want to move the indexed data out of the main tables since myisam doesn't support things like foreign keys or transactions.

For more advanced free text indexing you could try sphinx (have mysql look-and-feel interface too) or solr.

Comments

1

If you're using MyISAM or innoDB, you can use the MySQL fulltext search:

SELECT * FROM table_name WHERE MATCH (title) AGAINST ('love hobby' IN BOOLEAN MODE);

It'll also search for individual words as well.

Read this: https://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

Comments

0

You can also use MySQL REGEXP

SELECT title FROM table WHERE title REGEXP ' love .+ hobby';

If you have it as a single string then try:

SELECT title FROM table WHERE title REGEXP REPLACE('love hobby', ' ', '.+ ');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.