在mysql的两列中仅显示不同的值

时间:2018-06-30 10:44:06

标签: mysql mysql-workbench

SELECT 
    DISTINCT (CONCAT_WS('~', t5.component, t5.value)) AS inv_component,
    CONCAT_WS('~', t6.component, t6.value) AS comp_component    
FROM tds_salary_table t4 
    LEFT JOIN tds_investment_table t5 ON t4.employee_code = t5.employee_code
    LEFT JOIN tds_computation_table t6 ON t4.employee_code = t6.employee_code   
WHERE  
    t4.employee_code = 'E03135' AND t4.status !='2'AND t5.status !='2' AND t6.status !='2' 
GROUP BY inv_component, comp_component

电流输出

Value1                             value2

PPF DEPOSIT~2000.00    ADD: EDUCATION + HEALTH CESS 4%~10000.00
PPF DEPOSIT~2000.00    BALANCE TAX PAYABLE~12.00
PPF DEPOSIT~2000.00    DEDUCTION U/S 80 C~5000.00
PROVIDENT FUND~1000.00     ADD: EDUCATION + HEALTH CESS 4%~10000.00
PROVIDENT FUND~1000.00     BALANCE TAX PAYABLE~12.00
PROVIDENT FUND~1000.00     DEDUCTION U/S 80 C~5000.00

但是我的输出应该是这样

Value1                             value2

PPF DEPOSIT~2000.00    ADD: EDUCATION + HEALTH CESS 4%~10000.00
PROVIDENT FUND~1000.00     BALANCE TAX PAYABLE~12.00
Null                      DEDUCTION U/S 80 C~5000.00

1 个答案:

答案 0 :(得分:0)

您可以像这样使用group_concat:

SELECT 
    DISTINCT (CONCAT_WS('~', t5.component, t5.value)) AS inv_component,
    GROUP_CONCAT(CONCAT_WS('~', t6.component, t6.value)) AS comp_component    
FROM tds_salary_table t4 
    LEFT JOIN tds_investment_table t5 ON t4.employee_code = t5.employee_code
    LEFT JOIN tds_computation_table t6 ON t4.employee_code = t6.employee_code   
WHERE  
    t4.employee_code = 'E03135' AND t4.status !='2'AND t5.status !='2' AND t6.status !='2' 
GROUP BY inv_component