我有 3 张 table ,这是我的 table 1,成员:
+---------+ | user_id | +---------+ | 1 | +---------+
and my table 2,Members7:
+---------------+---------------------------+---------+ | TotalWorkYear | JobScope | user_id | +---------------+---------------------------+---------+ | 1 | Financial;pharmacy | 1 | | 6 | Doctor/Diagnosis;pharmacy | 1 | | 10 | Accounting;pharmacy | 1 | +---------------+---------------------------+---------+
And table 3,Members7_2:
+-----------------+----------------+---------+ | TotalWorkYear_2 | KnowledgeSkill | user_id | +-----------------+----------------+---------+ | 1 | abc;def; | 1 | | 6 | vw;xyz;def | 1 | | 10 | vw;xyz; | 1 | +-----------------+----------------+---------+
I need to find out the user_id that regexp 'pharmacy' in JobScope
of Members7
,and the total years of JobScope
that regexp pharmacy is >=0,
and also regexp 'def' in KnowledgeSkill
of Members7_2
,and the total years of KnowledgeSkill
that regexp 'def' is >=0,
so now we can see that:
TotalWorkYear = 17
TotalWorkYear_2 = 7
user_id = '1'
这是我的查询:
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 行的 TotalWorkYears
求和。
有什么办法可以解决吗?
最佳答案
您对多个表的左联接将使某些技能多次计算。重写查询的简单方法是使用其他表的子查询;
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
作为尾注,如果您的表变大,REGEX
不是一个好方法,您至少需要将分号分隔的值分成行,以便索引/完全匹配可以覆盖它们。
关于mysql - LEFT JOIN 表和 REGEXP 特定字符的 SUM 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17563693/