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

cngwdvgl  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(389)
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
ehxuflar

ehxuflar1#

您可以这样使用组\u 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

相关问题