Domanda

I need help on creating a full text search in multiple columns.

At the end the query should return 'biz_name', first match against 'business' table then match against 'tags' table then select biz_id from 'Business_tags' table then put together all with prioritize results from 'Business Table'.

In easy explanation, when someone search for bank, they should able to get business name that has 'bank' in it and business that tagged with 'bank'.

Please consider the three tables:

Business Table and Tags Table

|biz_id |biz_name      |       |tag_id |tag_name|    
|----------------------|       |----------------|
|001    |Mac burger    |       |101    |cafe    |
|002    |Citi bank     |       |102    |burger  |
|003    |Lou’s Cafe    |       |103    |drink   |
|004    |Baltic pub    |       |104    |bank    |
|005    |T2 Restaurant |       |105    |loan    |

Business_tags Table

|biz_id |tag_id |           
|---------------|       
|001    |102    |       
|002    |105    |       
|002    |104    |       
|003    |101    |       
|003    |103    |       
|004    |103    | 
|005    |102    | 
|005    |101    | 

I can't not use union cause I've different number of columns. Thanks for the help.

È stato utile?

Soluzione

You can join the three tables together and also do a text search on the name.

SELECT business.biz_name
   FROM business,business_tags,tags
   WHERE business.biz_id = business_tags.biz_id
   AND business_tags.tag_id = tags.tag_id
   AND (business.biz_name LIKE '%bank%' OR tags.tag_name = 'bank')

Will that do what you want?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top