简体   繁体   中英

Combine varchar rows of MySql using Union query

I have two tables called department and function.

department

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

function

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

This is my query

 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

The output of this query is

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

But what I want is

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

Is it possible to get this output?

You can use the following solution using INNER JOIN instead of UNION :

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 

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

Try this, it uses JOIN to achieve your goal:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM