我有以下表结构:
cv1 cv2 cv_content1 cv_content2
abc xyz php,mysql sql dotnet
aaa xxx java php is my skill
我有两个关键字让我们说php和dotnet。现在我想从上面的表t1获取记录,其中前面提到的两个关键字在列cv_content1和cv_content2中有OR条件。我想要下面的记录:
cv1 cv2
abc xyz
--- xxx
我尝试了case语句,如果还有,我还要检查有多少关键字匹配记录,例如第一行结果是100%(php和dotnet都在行中)但是对于第二行它是50%(只有php匹配)。
到目前为止,我尝试了以下查询:
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[[:>:]]')
它也有语法错误。不明白如何使用if语句
答案 0 :(得分: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
答案 1 :(得分:0)
使用find_in_set
搜索并OR
查找任何输入值。
示例:
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
修改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;
演示 @ MySQL Fiddle
编辑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;
演示 @ MySQL Fiddle