mysql - LEFT JOIN 表和 REGEXP 特定字符的 SUM 值

标签 mysql sql regex

我有 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

An SQLfiddle to test with .

作为尾注,如果您的表变大,REGEX 不是一个好方法,您至少需要将分号分隔的值分成行,以便索引/完全匹配可以覆盖它们。

关于mysql - LEFT JOIN 表和 REGEXP 特定字符的 SUM 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17563693/

相关文章:

sql - 从 Oracle 迁移到 PostgreSQL 时对 sibling BY 和 rownum 进行 ORDER

.net - 使用 SqlCommand 执行非查询,您可以获取通常发布到 "Messages"的文本吗?

Java:从一个字符串值替换多个字符串模式

MySQL 查询运行*非常*慢

mysql - 在 MYSQL 中搜索 JSON 字段中的值

mysql - MySQL 中的 JSON_SEARCH

mysql - 无法连接到生产环境中的 MySQL 数据库

sql - Redshift 数据库的透视

regex - SVN 标签版本号从命令行递增

regex - Perl 问号后跟冒号模式匹配