MYSQL查询使用CONTAINS更新字段

时间:2014-05-22 10:31:01

标签: mysql

我想在mySQL中进行查询,如果department_name包含在“departments_handling_the_process”字段中,我需要使用process_table的“process_name”中的连接内容更新department_table的“processes_handled”。

这就是我的问题:

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)
;

我希望你能帮我解决问题。谢谢,更有力量!

1 个答案:

答案 0 :(得分: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 |

所以要做好重度测试和调试的准备。此外,在其他情况下,您可能会被迫编写一个函数来拆分逗号分隔列并将其分布在多行上。 stackoverflow上有足够的示例,这是唯一的选择。再次(但这次是最后一次),你将在这个数据库设计上遇到很多麻烦。

  • 详细了解所使用的功能here
  • 看到它在 sqlfiddle
  • 中正常工作