在mysql列中计数条目并显示计数

7cwmlq89  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(339)

我想计算列中的条目数并在旁边显示计数。然而,我不知道如何才能做到这一点。
期望输出:

arrangement_number      tray_no  rl_type  flag(count of occurrence)
------------------      -------  ----     ----
2774818                 381001   R        3
2774818                 381001   R        3
2774818                 381001   L        3
2778470                 405128   R        1
2779702                 265265   R        2
2779702                 265265   R        2

我正在尝试使用 @variables 但我还是做不好。每一行都是唯一的,我不需要对它们进行分组。
更新:扩展表添加了源代码
注意:我现在加入5个表
实际查询:

SELECT 
    log.arrangement_number,
    header.tray_number,
    detail.rl_type,

    -- some more fields here

FROM
    log
        INNER JOIN
    header ON log.arrangement_number = header.rxarrangement_number
        AND log.production_place_code = header.production_place_code
        INNER JOIN
    detail ON log.arrangement_number = detail.rxarrangement_number
        AND log.production_place_code = detail.production_place_code
        INNER JOIN
    deliveryperiod ON log.arrangement_number = deliveryperiod.arrangement_number
        AND log.production_place_code = deliveryperiod.production_place_code
        AND detail.rl_type = deliveryperiod.rl_type
        INNER JOIN
    calc ON calc.arrangement_number = log.arrangement_number
        AND calc.production_place_code = log.production_place_code
        AND deliveryperiod.rl_type = calc.rl_type
        AND detail.rl_type = calc.rl_type
WHERE
    header.status_code IN ('20' , '21')
        AND log.process_code = '12'
        AND deliveryperiod.process_code_current = '12'
        AND deliveryperiod.sub_process_code_current IN ('100' , '105')
        AND lot_number = '120131'
ORDER BY log.lot_number , log.sequence_number , deliveryperiod.rl_type DESC
mccptt67

mccptt671#

试试这个。。。

SELECT tray_no, COUNT(*) 'flag'
FROM table1
GROUP BY tray_no
35g0bw71

35g0bw712#

SELECT t1.tray_no,
       t2.flag
FROM yourTable
INNER JOIN
(
    SELECT tray_no, COUNT(*) AS flag
    FROM yourTable
    GROUP BY tray_no
) t2
    ON t1.tray_no = t2.tray_no

相关问题