使用联合查询合并MySql的varchar行

时间:2018-08-23 06:34:41

标签: mysql union

我有两个表,分别是Department和Function。

部门

------------------------
dept_id   |  dept_name
------------------------
  1       |  department1
  2       |  department2
------------------------

功能

--------------------------------------------------------------
function_id  |  function_name  |  dept_id  | is_main_function
--------------------------------------------------------------
    1        |  function1      |    1      |       1
    2        |  function2      |    1      |       0
    3        |  function3      |    1      |       0
--------------------------------------------------------------

这是我的查询

 select dept_name,function_name as main_function,null as sub_function from department d,functon f where d.dept_id = f.dept_id and is_main_function = 1
 union 
 select dept_name,null as main_function,function_name as sub_function from department d,functon f where d.dept_id = f.dept_id and is_main_function = 0

此查询的输出为

------------------------------------------------
dept_name    |  main_function  |  sub_function 
------------------------------------------------
department1  |    function1    |    null
department1  |     null        |   function2      
department1  |     null        |   function2      
------------------------------------------------

但是我想要的是

------------------------------------------------
dept_name    |  main_function  |  sub_function 
------------------------------------------------
department1  |    function1    |   function2      
department1  |    function1    |   function2      
------------------------------------------------

是否可以获取此输出?

2 个答案:

答案 0 :(得分:1)

您可以使用INNER JOIN代替SELECT d.dept_name, f1.function_name AS main_function, f2.function_name AS sub_function FROM department d INNER JOIN `function` f1 ON d.dept_id = f1.dept_id AND f1.is_main_function = 1 INNER JOIN `function` f2 ON d.dept_id = f2.dept_id AND f2.is_main_function = 0 使用以下解决方案:

from nltk.tokenize import word_tokenize
message = "I am holding a B.tech, M.Tech and Ph.D in Computer Science"
degrees = ["btech", "bsc", "mtech", "phd"]
tokens = word_tokenize(message)
for token in tokens:
    for character in string.punctuation:
        token = token.replace(character, "")
    token = token.lower()
    if token in degrees:
        print(token)
  

演示: https://www.db-fiddle.com/f/owQzS9C1bnGF4zwCSWtirF/1

答案 1 :(得分:0)

尝试一下,它使用JOIN来实现您的目标:

declare @department table (dept_id int, dept_name varchar(20));
insert into @department values
 (1 , 'department1'),
 (2 , 'department2');

declare @function table (function_id int, function_name varchar(20), dept_id int, is_main_function int);
insert into @function values
 (1 , 'function1' , 1 , 1),
 (2 , 'function2' , 1 , 0),
 (3 , 'function3' , 1 , 0);

 select d.*, main.function_name, sub.function_name
 from @department d
 join @function main on d.dept_id = main.dept_id and main.is_main_function = 1
 join @function sub on d.dept_id = sub.dept_id and sub.is_main_function = 0