-2

I have below table structure:

cv1    cv2   cv_content1   cv_content2
abc    xyz     php,mysql     sql dotnet
aaa    xxx     java          php is my skill

I have two keywords lets say php and dotnet. Now I want to fetch record from above table t1 with two previous mentioned keywords in column cv_content1 and cv_content2 with OR condition. I want below records:

cv1   cv2
abc    xyz  
---    xxx

I tried case statement and if also and I also have to check that how many keywords match for a record for example for first row of result it is 100%(php and dotnet both are in row) but for second row it is 50%(only php matched).

I tried below query so far:

    SELECT ROUND(( ( if(LOCATE("php",cv_content1 )>0,25,0) OR if(LOCATE("php",cv_content2 )>0,25,0) ) + if(LOCATE("dotnet",cv_content1 )>0,25,0) OR if(LOCATE("dotnet",cv_content2 )>0,25,0) ) * 100 ) / 50) as SkillTot,if (own_content regexp '[[:<:]]smith[[:>:]]',`own_filename`),if (kings_content regexp '[[:<:]]smith[[:>:]]' ,`kings_filename`) FROM `t1`WHERE ( cv_content1 REGEXP '[[:<:]]php[[:>:]]' OR cv_content2 REGEXP '[[:<:]]php[[:>:]]' OR cv_content1 REGEXP '[[:<:]]dotnet[[:>:]]' OR cv_content2 REGEXP '[[:<:]]dotnet[[:>:]]') 

It has a syntax error also. Don't understand how to use if statement

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1
SELECT IF(FIND_IN_SET('php', cv_content1) OR FIND_IN_SET('dotnet', cv_content1),
          cv1, NULL) AS cv1,
       IF(FIND_IN_SET('php', cv_content2) OR FIND_IN_SET('dotnet', cv_content2),
          cv2, NULL) AS cv2
FROM YourTable
HAVING cv1 IS NOT NULL OR cv2 IS NOT NULL
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Use find_in_set to search and OR to find any of the input values.

Example:

select cv1, cv2
  from t1
 where find_in_set( 'php', concat( cv_content1, ',', cv_content2 ) ) > 0 
    or find_in_set( 'dotnet', concat( cv_content1, ',', cv_content2 ) ) > 0 

Edit 1:

select 
   case when find_in_set( 'php', cv_content1 ) 
          or find_in_set( 'dotnet', cv_content1 ) 
        then cv1 else '---' end as cv1
 , case when find_in_set( 'php', cv_content2 ) 
          or find_in_set( 'dotnet', cv_content2 ) 
        then cv2 else '---' end as cv2
from t1;

Demo @ MySQL Fiddle


Edit 2:

select 
   case when find_in_set( 'php', replace( cv_content1, ' ', ',' ) ) 
          or find_in_set( 'dotnet', replace( cv_content1, ' ', ',' ) ) 
        then cv1 else '---' end as cv1
 , case when find_in_set( 'php', replace( cv_content2, ' ', ',' ) ) 
          or find_in_set( 'dotnet', replace( cv_content2, ' ', ',' ) ) 
        then cv2 else '---' end as cv2
from t1;

Demo @ MySQL Fiddle

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82