LEFT JOIN表和SUM值表示REGEX特定字符

时间:2013-07-10 06:20:06

标签: mysql sql regex

我有3张桌子,这是我的桌子1,成员:

+---------+
| user_id |
+---------+
|       1 |
+---------+

和我的表2,会员7:

+---------------+---------------------------+---------+
| TotalWorkYear |         JobScope          | user_id |
+---------------+---------------------------+---------+
|             1 | Financial;pharmacy        |       1 |
|             6 | Doctor/Diagnosis;pharmacy |       1 |
|            10 | Accounting;pharmacy       |       1 |
+---------------+---------------------------+---------+

表3,成员7_2:

+-----------------+----------------+---------+
| TotalWorkYear_2 | KnowledgeSkill | user_id |
+-----------------+----------------+---------+
|               1 | abc;def;       |       1 |
|               6 | vw;xyz;def     |       1 |
|              10 | vw;xyz;        |       1 |
+-----------------+----------------+---------+

我需要在JobScope的{​​{1}}中找到user_id regexp'pharmacy',并且regexp药房的总年数为Members7> = 0, 以及JobScope KnowledgeSkill中的regexp'def',以及regexp'def'为> = 0的Members7_2的总年数,

现在我们可以看到:

KnowledgeSkill

这是我的疑问:

SELECT Members.user_id, 
SUM( IF( Members7.JobScope
REGEXP 'Pharmacy', Members7.TotalWorkYear, 0 ) ) yearsJobScope, Members7.JobScope AS JobScope, 
SUM( IF( Members7_2.KnowledgeSkill
REGEXP 'def', Members7_2.TotalWorkYear_2, 0 ) ) yearsSkill, Members7_2.KnowledgeSkill AS KnowledgeSkill
FROM Members
LEFT JOIN Members7 ON Members.user_id = Members7.user_id
LEFT JOIN Members7_2 ON Members.user_id = Members7_2.user_id
GROUP BY JobScope REGEXP 'Pharmacy', 
KnowledgeSkill REGEXP 'def', user_id
HAVING 
JobScope REGEXP 'Pharmacy'
AND yearsJobScope >=0
AND KnowledgeSkill REGEXP 'def'
AND yearsSkill >=0

但输出是:

+---------------+------------+
| yearsJobScope | yearsSkill |
+---------------+------------+
|            17 |        18  |
+---------------+------------+

如果我在HAVING下删除了REGEXP,我可以看到:

+---------+---------------+---------------------------------------------+------------+----------------+
| user_id | yearsJobScope |                  JobScope                   | yearsSkill | KnowledgeSkill |
+---------+---------------+---------------------------------------------+------------+----------------+
|       1 |             1 | Audit & Taxation;Banking/Financial;pharmacy |          6 | vw;xyz;def     |
|       1 |             6 | Doctor/Diagnosis;pharmacy                   |          6 | vw;xyz;def     |
|       1 |            10 | General/Cost Accounting;pharmacy            |          6 | vw;xyz;def     |
+---------+---------------+---------------------------------------------+------------+----------------+

似乎只是对3行的TotalWorkYear = 17 TotalWorkYear_2 = 7 user_id = '1' 求和。

有什么办法可以解决吗?

1 个答案:

答案 0 :(得分:3)

您与多个表的左连接将使一些技能不止一次。重写查询的简便方法是使用其他表的子查询;

SELECT m.user_id, yearsjobscope, yearsskill
FROM members m
JOIN (
  SELECT user_id, SUM(totalworkyear) yearsjobscope
  FROM members7 WHERE jobscope REGEXP 'Pharmacy'
  GROUP BY user_id) m7
  ON m.user_id = m7.user_id
JOIN (
  SELECT user_id, SUM(totalworkyear_2) yearsskill
  FROM members7_2 WHERE KnowledgeSkill REGEXP 'def'
  GROUP BY user_id) m7_2
  ON m.user_id = m7_2.user_id

An SQLfiddle to test with

最后要注意的是,如果你的表变大,REGEX不是一个好方法,你需要至少将分号分隔的值拆分成行,这样索引/完全匹配就可以了覆盖他们。