search

Home  >  Q&A  >  body text

mysql - sql query statistics

![I need to count the U and A corresponding to each layerid. For example,
HYD_NET_LN U 5 A 10
HYD_VAL_PT U 8 A 25
How to write
][1]

PHPzPHPz2756 days ago886

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-05-18 10:55:38

    Keep it simple, I don’t know if this is possible.
    SELECT layerid,ChangeMold,COUNT(ChangeMold) FROM table_name
    GROUP BY layerid,ChangeMold

    reply
    0
  • 迷茫

    迷茫2017-05-18 10:55:38

    select count(*),layid,changeMold from tbl group by layid,changeMold

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-18 10:55:38

    SELECT 
    layerid,
    sum(case when changeMold='U' then 1 else 0 end) changeMold_U,
    sum(case when changeMold='A' then 1 else 0 end) changeMold_A
    FROM table_name 
    GROUP BY layerid;

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-18 10:55:38

    If you want to count such large files. It is better to check them all and then process the array more conveniently (sql is relatively weak)

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-18 10:55:38

    select layerid,changeMold,count(*) as num from TABLE where layerid in (select layerid from TABLE group by layerid) group by changeMold; The performance of this SQL is very poor

    reply
    0
  • Cancelreply