2014-05-22 130 views
0

我想在mySQL中進行查詢,如果department_name包含在「departments_handling_the_process」中,需要使用process_table的「process_name」的連接內容更新department_table的「processes_handled」領域。MYSQL查詢使用CONTAINS更新字段

這是我的問題是什麼樣子:

department_table  

department_name | processes_handled 
Department 1 | Process A; Process B; Process D; 
Department 2 | Process A; Process C: Process E; Process G; 
Department 3 | Process C; Process D; Process F 




process_table 

process_name | departments_handling_the_process 
Process A | Department 1; Department 2 
Process B | Department 1 
Process C | Department 2; Department 3 
Process D | Department 1; Department 3 
Process E | Department 2 
Process F | Department 3 
Process G | Department 2 

我現在的更新語句如下:

UPDATE department_table,process_table 
SET processes_handled= (SELECT GROUP_CONCAT(`process_name SEPARATOR '; ') 
         FROM process_table 
         WHERE CONTAINS(process_table.departments_handling_the_process, department_table.department_name)) 

WHERE CONTAINS(process_table.departments_handling_the_process, department_table.department_name) 
; 

我希望你能幫助我解決我的發言。感謝和更多的權力!

+0

帶電作業請閱讀本功能:http://stackoverflow.com/a/3653574/447489 – fancyPants

+0

謝謝你的迴應!不過,我在數據庫列中的分隔列表中沒有問題。 – Rezard

+0

還沒...相信我。 – fancyPants

回答

0

假設基於此評論:

好吧,但你的問題是目前尚不清楚給我。期望的結果是什麼?在我看來,department_table中的process_handled列已經是理想的結果。或者現在是空的?

的樣本數據:

CREATE TABLE department 
    (`department_name` varchar(12), `processes_handled` varchar(43)) 
; 


INSERT INTO department 
    (`department_name`, `processes_handled`) 
VALUES 
    ('Department 1', ''), 
    ('Department 2', ''), 
    ('Department 3', '') 
; 


CREATE TABLE process 
    (`process_name` varchar(9), `departments_handling_the_process` varchar(26)) 
; 

INSERT INTO process 
    (`process_name`, `departments_handling_the_process`) 
VALUES 
    ('Process A', 'Department 1; Department 2'), 
    ('Process B', 'Department 1'), 
    ('Process C', 'Department 2; Department 3'), 
    ('Process D', 'Department 1; Department 3'), 
    ('Process E', 'Department 2'), 
    ('Process F', 'Department 3'), 
    ('Process G', 'Department 2') 
; 

解決方案/更新語句:

update 
department 
set processes_handled = (select group_concat(process_name separator '; ') 
         from process where find_in_set(department_name, replace(departments_handling_the_process, '; ', ',')) != 0); 

結果:

select * from department; 

| DEPARTMENT_NAME |       PROCESSES_HANDLED | 
|-----------------|--------------------------------------------| 
| Department 1 |   Process A; Process B; Process D | 
| Department 2 | Process A; Process C; Process E; Process G | 
| Department 3 |   Process C; Process D; Process F | 

說明:

MySQL中沒有contains函數。改爲使用find_in_set()。還請注意,您必須用,替換;,以便find_in_set()正常工作。你可能會碰到更多這樣的「陷阱」。有些你會注意到,有些你可能不會(一次)。下面是留下了replace()功能,當輸出:

| DEPARTMENT_NAME | PROCESSES_HANDLED | 
|-----------------|----------------------| 
| Department 1 |   Process B | 
| Department 2 | Process E; Process G | 
| Department 3 |   Process F | 

所以要重測試和調試準備。此外,在其他情況下,您可能會被迫編寫一個函數來分割逗號分隔列並將其分散到多行中。這裏有足夠的例子在這裏是唯一的選擇。再次(但這次是最後一次),這個數據庫設計會遇到很多麻煩。

+0

非常感謝。我用下面的用戶定義的函數來隔離的分隔的字符串:CREATE FUNCTION SPLIT_STR( X VARCHAR(255), DELIM VARCHAR(12), POS INT ) RETURNS VARCHAR(255) RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX (x,delim,pos), LENGTH(SUBSTRING_INDEX(x,delim,pos -1))+ 1), delim,''); – Rezard