我必须进行查询才能显示我的gridview的数据。我正在计算每个票证类别的票证数量,但是如果该类别的票证数量少于3,则需要将其放入“其他”类别。

Select
count(ticket.id) As 'Tickets',
if(count(ticket.id) <= 3, "Other",ticketCategoryName) As 'Ticket   Category'
FROM ds_tickets
GROUP BY 'Ticket Category


而不是像这样:

+----------------+-------------+
| TicketCategory | TicketCount |
+----------------+-------------+
| Category1      |           2 |
| Category2      |           3 |
| Category3      |           4 |
| Category4      |           5 |
+----------------+-------------+


我希望它像:

+----------------+-------------+
| TicketCategory | TicketCount |
+----------------+-------------+
| Other          |           5 |
| Category3      |           4 |
| Category4      |           5 |
+----------------+-------------+

最佳答案

试试这个

SELECT CASE WHEN CNT <= 3 THEN 'Other' ELSE TicketCategory END 'Ticket Category' ,
      SUM(CNT)
FROM(
    SELECT  TicketCategory, COUNT(*) as CNT
    FROM Table
    GROUP BY  TicketCategory
)M
GROUP BY CASE WHEN CNT <= 3 THEN 'Other' ELSE TicketCategory END

关于mysql - 当计数<= 3时更改另一列的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39577825/

10-11 20:11