0

如果我有如下表格:

年 | 职位 | 技能 | 用户身份

    1 | j1;j2 | s1 | 1

    2 | j3 | s2 | 1

    1 | j1 | s2;s3 | 1

    3 | j2 | s1 | 1

当我进行过滤时,我希望

select user_id FROM Table WHERE

1. (Job regexp 'j1' AND TotalYears of Job REGEXP 'j1' >2),

2. (Skill regexp 's1' AND TotalYears of Skill regexp 's1' >3),

所以首先我需要先找出 Job regexp 'j1' 的总年限以及 Skill regexp 's1' 的总年限。

j1 = 2 年,s1 = 4 年

如果我只过滤 Job regexp 'j1' ,我的查询将是:

SELECT Sum(Years) AS Years,Job AS Job FROM Table WHERE Job REGEXP 'j1' HAVING Years>2;

但现在我还需要过滤技能,我应该如何编辑我的查询?

谢谢你。

PS:不知道题主是否正确,如有不妥之处请指正,谢谢。

编辑(2013-07-10):

我的新表:

总工作年 | 工作范围 | 用户身份

1 | 审计与税务;银行/金融;二 | 1

4 | 出版;其他;| 1

0 | 一般/成本会计;二 | 1

0 | 伊芙琳 | 1

这是我的查询:(我离开了 2 个表,成员和成员 7,这并不重要)

SELECT Members.user_id,SUM(IF(Members7.JobScope REGEXP 'eve',Members7.TotalWorkYear,0)) yearsJobScope,Members7.JobScope AS JobScope FROM Members LEFT JOIN Members7 ON Members.user_id=Members7.user_id GROUP BY user_id Having JobScope REGEXP 'eve' AND yearsJobScope >= 0

虽然有一行正则表达式'eve'并且totalworkyears> = 0,但它显示零结果,我认为是由用户ID分组引起的,所有记录仅根据用户ID分组。有没有办法解决它?谢谢。

4

2 回答 2

1
  SELECT user_id,
         SUM(IF(job LIKE '%j1%',years,0)) yearsJob,
         SUM(IF(skill LIKE '%s1%', years,0)) yearsSkill
    FROM yourTable
GROUP BY user_id
  HAVING yearsJob>2
     AND yearsSkill>3;
于 2013-07-07T15:59:10.377 回答
1

尝试关注查询。它会正常工作。

SELECT *
FROM
(
    SELECT USERID
    FROM TABLE_3
    WHERE JOBS LIKE '%j1%'
    GROUP BY USERID 
    HAVING SUM(YEARS) > 0
) AS A
INNER JOIN 
(
    SELECT USERID
    FROM TABLE_3
    WHERE SKILL LIKE '%S1%'
    GROUP BY USERID 
    HAVING SUM(YEARS) > 0

) AS B ON A.USERID = B.USERID    

sqlfiddle

于 2013-07-07T16:00:46.007 回答