简体   繁体   English

如何从MySQL中的单个表中获取不同的计数

[英]how to take different counts from single table in MySQL

I have a table named - studentId_t which is having fields: 我有一个名为studentId_t的表,其中包含字段:

  • SrNo. SrNo。
  • classId 班级号
  • StudentId 学生卡
  • StudentName 学生姓名
  • Status 状态
  • Creation Date 创建日期
  • Modified Date 修改日期

and another table named studentE which is having fields 另一个名为studentE表具有字段

  • SrNo. SrNo。
  • classId 班级号
  • studentId 学生卡
  • studentAdd studentAdd
  • EId 开斋节

-- I want to check status of student and I am having only E_id -我想检查学生的状态,我只有E_id

I was trying query like .. 我正在尝试查询..

SELECT COUNT(1) AS ST FROM `StuInfo`.`studentId_t` S, `StuInfo`.`studentE` SE
    WHERE S.`status` = 'ST'
    AND SE.`studentId` = S.`studentId`
    AND SE.`EId` = 1

but here, I want to calculate status in (ST, BS, BH, GN), So how can i calculate it in single query from above query I am able to calculate single status, I want multiple status 但是在这里,我想在(ST,BS,BH,GN)中计算状态,所以如何从上述查询中在单个查询中计算它我能够计算单个状态,我想要多个状态

USE SUM() with if,else conditions oriented to all status. SUM()if,else条件面向所有状态一起使用。

Try like below: 尝试如下:

SELECT SUM(if(status='ST',1,0))  AS ST , SUM(if(status='BS',1,0))  AS   BS,
SUM(if(status='BH',1,0))  AS BH,SUM(if(status='GN',1,0))  AS GN
FROM StuInfo.studentId_t S, StuInfo.studentE SE
WHERE S.status IN ('ST', 'BH','BS','GN') AND SE.studentId = S.studentId AND SE.EId = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM