我有一个Mysql DB,我需要将show输出到前端。我不想在Java代码中进行迭代,而是希望从查询输出中获取自定义列。

 Table: EmpTable----------------------------------EmpId | DaysCameToOffice----------------------------------101 | Monday101 | Tuesday102 | Monday103 | Monday102 | Wednesday102 | Thursday--------------------------------

 Desired output (as in Query result)---------------------------------------------------------EmpId | displayEmp--------------------------------------------------------101 | 101 (2 records)102 | 102 (3 records)103 | 102 (1 records)---------------------------------------------------------

TRIED查询

 SELECT m.EmpId as EmpId, CONCAT(m.EmpId, " (", COUNT(SELECT DISTINCT(c.DaysCameToOffice) from EmpTable c, " records)") as displayEmp FROM EmpTable m

给我:-错误代码:1064,SQL状态:42000]您的SQL语法有错误

请帮助我

最佳答案

count()适用于表达式,而不适用于查询,因此您需要将其复杂化很多:

select empid, concat(empid, ' (', count(distinct DaysCameToOffice),' records)') as displayEmp
from EmpTable
group by empid


但是,我将在没有连接的情况下运行查询,仅在计数时运行查询,并在呈现结果的应用程序中执行此连接。

关于mysql - MYSQL如何将一列与另一列的计数连接为数据库的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43896251/

10-11 03:20