简体   繁体   中英

How to get MYSQL relevance search result

i am the new user of mysql and i want to relevant search result by a sentence search or keyword search like google search.

Table Name: affiliate_products

id                      product_name
---                     ------------
1                       smartphone micromax 2gb ram
2                       smartphone samsung 2gb ram
3                       laptop sony ram
4                       lenovo a361 smartphone
5                       sony z1 dual core smartphone
6                       Sony HD Dual Sim Smartphone

i have already used some sql like below

SELECT *,

    CASE
      WHEN product_name LIKE '%vbsdf 2gb ram%'  THEN 50
      WHEN product_name LIKE '%vbsdf%2gb%ram%'  THEN 40
      WHEN product_name LIKE '%vbsdf%' AND product_name LIKE '%2gb%' AND product_name LIKE '%ram%' THEN 30
      WHEN product_name LIKE '%vbsdf%'  THEN 20
      WHEN product_name LIKE '%2gb%'  THEN 15
      WHEN product_name LIKE '%ram%'  THEN 10
      ELSE 0
    End as Importance

    FROM `affiliate_products`
    WHERE `product_name` LIKE '%vbsdf%'
    OR `product_name` LIKE '%2gb%'
    OR `product_name` LIKE '%ram%'
    ORDER BY Importance DESC;

And also used

Select `product_name` from `affiliate_products` WHERE match(`product_name`) against('+"vbsdf" +2gb' +'ram' in boolean mode)

but this sql query not meet my requirement.

i want to get result by below

If Search Keyword: Micromax Ram

    id                      product_name
    ---                     ------------
    1                       smartphone **micromax** 2gb **ram**
    2                       smartphone samsung 2gb **ram**
    3                       laptop sony **ram**

inside ** ** keyword has been matched

If Search Keyword : 2gb Smartphone

id                      product_name
---                     ------------
1                       **smartphone** micromax **2gb** ram
2                       **smartphone** samsung **2gb** ram
4                       lenovo a361 **smartphone**
5                       sony z1 dual core **smartphone**
6                       Sony HD Dual Sim **Smartphone**

inside ** ** keyword has been matched

if Search Keyword : Quad Core Smartphone

id                      product_name
---                     ------------
5                       sony z1 dual **core** **smartphone**
1                       **smartphone** micromax 2gb ram
2                       **smartphone** samsung 2gb ram
4                       lenovo a361 **smartphone**
6                       Sony HD Dual Sim **Smartphone**

inside ** ** keyword has been matched

if Search Keyword : buy sony quad core ram smartphone

id                      product_name
---                     ------------
5                       **sony** z1 dual **core** **smartphone**
6                       **Sony** HD Dual Sim **Smartphone**
3                       laptop **sony** **ram**
1                       **smartphone** micromax 2gb ram
2                       **smartphone** samsung 2gb ram
4                       lenovo a361 **smartphone**

inside ** ** keyword has been matched

please help me how i can make this sql to get the this kind of information result. Thanks in advance.

thanks to all the answer submitter. I have got the answer and i have implement this and it is working fine and the sql query is

    SELECT *,
       CASE
           WHEN product_name LIKE '%smartphone a361%' THEN 6
           WHEN MATCH (product_name) AGAINST ('+smartphone +a361' IN BOOLEAN MODE) THEN 5
           WHEN product_name LIKE '%smartphone%a361%' THEN 4
           WHEN product_name LIKE '%smartphone%' THEN 2
           WHEN product_name LIKE '%a361%' THEN 1
           ELSE 0
       END AS Importance
FROM `affiliate_products`
WHERE status = 'Active'
  AND (product_name LIKE '%smartphone%'
       OR product_name LIKE '%a361%')
ORDER BY Importance DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM