我有3個表,這裏是我的表1,會員:LEFT JOIN表和和值是REGEXP特定字符
+---------+ | user_id | +---------+ | 1 | +---------+
和我的表2,Members7:
+---------------+---------------------------+---------+ | TotalWorkYear | JobScope | user_id | +---------------+---------------------------+---------+ | 1 | Financial;pharmacy | 1 | | 6 | Doctor/Diagnosis;pharmacy | 1 | | 10 | Accounting;pharmacy | 1 | +---------------+---------------------------+---------+
而且表3,Members7_2:
+-----------------+----------------+---------+ | TotalWorkYear_2 | KnowledgeSkill | user_id | +-----------------+----------------+---------+ | 1 | abc;def; | 1 | | 6 | vw;xyz;def | 1 | | 10 | vw;xyz; | 1 | +-----------------+----------------+---------+
我需要找出USER_ID 是正則表達式 '藥房' 在的,總年的JobScope
該正則表達式藥房是> = 0,在Members7_2
KnowledgeSkill
並且還正則表達式「DEF」,並且總年的KnowledgeSkill
該正則表達式「DEF」是> = 0,
所以現在我們可以看到:
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 | +---------------+------------+
,如果我下已經移除了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
行。
有什麼方法可以解決它嗎?
您的查詢中還有第三個表「members」。 –
@Joachim Isaksson對不起,這是第一張桌子,但是因爲它是'user_id',所以我沒有粘貼它。我編輯了我的問題。 –