doukuiqian9911 2018-07-22 21:07
浏览 98

计算每个不同id的不同值

Version: mySQLi PHP 5.4.45

I'm trying to count the number of panels per unique ID. Instead, I'm getting the total number of tests per ID.

Before Query: (example)

eReq | panel_name | test
------------------------
 500 | tox        | Butabital
 500 | tox        | Amphetamine
 500 | tox        | MethAmp
 500 | chem       | CBC
 600 | tox        | Morphine
 600 | chem       | CBC

Query:

SELECT panel_name, count(*)
FROM `ordered_codes` 
JOIN samples ON ordered_codes.tox_id = samples.tox_id
JOIN orders_codes ON ordered_codes.code_id = orders_codes.id
GROUP BY panel_name

Current Results:

   panel_name | Count(*)
   ---------------------
   tox        | 4
   chem       | 2

Needed Results: (Numbers should be lower - counting panel_name by eReq)

panel_name | Count(*)
---------------------
tox        | 2
chem       | 2

I feel like this is very simple, I'm just overthinking at this point.

  • 写回答

1条回答 默认 最新

  • doufeinai6081 2018-07-22 21:11
    关注

    Are you looking for count(distinct)?

    SELECT panel_name, count(distinct ereq)
    FROM ordered_codes oc JOIN
         samples s 
         ON oc.tox_id = s.tox_id 
    GROUP BY panel_name
    

    I don't understand why there are two joins to ordered_codes, so I removed the second, assuming it is a typo. Your query will generate a syntax error.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度