sql统计某个字段的某个值出现的次数

直接上案例

需求:PayLog缴费表有个缴费方式的字段Method(1-营业厅、2-手机APP、3-微信小程序、4-微信公众号、5-支付宝),需要统计这缴费方式分别有几次

1、表:
在这里插入图片描述
2、mysql写法:

SELECT
	SUM(Method = 1) AS Business_Hall,
	SUM(Method = 2) AS Business_Hall,
	SUM(Method = 3) AS WeChat_Applet,
	SUM(Method = 4) AS WeChat_Official_Account,
	SUM(Method = 5) AS Alipay
FROM
	bus_paylog bp
WHERE
	1 = 1
AND bp.CustomerID = 1

3、运行结果:
在这里插入图片描述
4、mybatis的xml文件(可以不看,我自己用的)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ws.wssp.charging.mapper.PayMethodMapper">
	<resultMap id="BaseResultMap" type="com.ws.wssp.charging.model.PayMethod">
		<result column="CustomerID" property="customerId" jdbcType="INTEGER" />
		<result column="Method" property="method" jdbcType="INTEGER" />
		<result column="Intime" property="intime" jdbcType="TIMESTAMP" />
		<result column="OrganizationId" property="organizationId" jdbcType="INTEGER" />
			<result property="BusinessHall" column="BusinessHall" jdbcType="DECIMAL" />
			<result property="MobileAPP" column="MobileAPP" jdbcType="DECIMAL" />
			<result property="WeChatApplet" column="WeChatApplet" jdbcType="DECIMAL" />
			<result property="WeChatOfficialAccount" column="WeChatOfficialAccount" jdbcType="DECIMAL" />
			<result property="Alipay" column="Alipay" jdbcType="DECIMAL" />
	</resultMap>
	
		<select id="getMonthPayMethod" resultMap="BaseResultMap">
		select SUM(Method = 1)as BusinessHall,
		SUM(Method=2) as MobileAPP,
		SUM(Method=3) as WeChatApplet,
		SUM(Method=4) as WeChatOfficialAccount,
		SUM(Method=5) as Alipay 
		from view_paymethod vp where 1 = 1
		<if test="customerId != null">
			and vp.CustomerID=#{customerId,jdbcType=DECIMAL}
		</if>
		<if test="month != null and month  != '' ">
		and date_format(vp.Intime,'%Y-%m')= #{month,jdbcType=VARCHAR}
	    </if>
	    <if test="organizations != null and organizations != ''">
            and FIND_IN_SET(vp.OrganizationId, #{organizations,jdbcType=VARCHAR})
        </if>
	</select>
</mapper>

5、对应的实体类

package com.ws.wssp.charging.model;

import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * @author Eric
 * @params 1-营业厅、2-手机APP、3-微信小程序、4-微信公众号、5-支付宝
 */
public class PayMethod {

	private Integer customerId;

	private Integer method;

	private Integer organizationId;

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 前端时间字符串转java时间戳
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") // 后台时间戳转前端时间字符串(json对象)
	private Date intime;

	/**
	* 补充@Value()的用法:
	*	1、设置从配置文件读取distributeUrl的值:
	*	@Value("${distributeUrl}")
	*	private String distributeUrl;
	*	
	*	2、设置int型默认值为0,如下
	*	
	*   3、设置String类型默认值:
	* 	@Value("${myconfig:abcd}")	
	*	private String myconfig;
	*/
	@Value("${BusinessHall:0}")
	private int BusinessHall;

	@Value("${MobileAPP:0}")
	private int MobileAPP;

	@Value("${WeChatApplet:0}")
	private int WeChatApplet;

	@Value("${WeChatOfficialAccount:0}")
	private int WeChatOfficialAccount;

	@Value("${Alipay:0}")
	private int Alipay;

	public Integer getCustomerId() {
		return customerId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	public Integer getMethod() {
		return method;
	}

	public void setMethod(Integer method) {
		this.method = method;
	}

	public Integer getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(Integer organizationId) {
		this.organizationId = organizationId;
	}

	public Date getIntime() {
		return intime;
	}

	public void setIntime(Date intime) {
		this.intime = intime;
	}

	public int getBusinessHall() {
		return BusinessHall;
	}

	public void setBusinessHall(int businessHall) {
		BusinessHall = businessHall;
	}

	public int getMobileAPP() {
		return MobileAPP;
	}

	public void setMobileAPP(int mobileAPP) {
		MobileAPP = mobileAPP;
	}

	public int getWeChatApplet() {
		return WeChatApplet;
	}

	public void setWeChatApplet(int weChatApplet) {
		WeChatApplet = weChatApplet;
	}

	public int getWeChatOfficialAccount() {
		return WeChatOfficialAccount;
	}

	public void setWeChatOfficialAccount(int weChatOfficialAccount) {
		WeChatOfficialAccount = weChatOfficialAccount;
	}

	public int getAlipay() {
		return Alipay;
	}

	public void setAlipay(int alipay) {
		Alipay = alipay;
	}
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用以下 SQL 查询来实现每个人员提交的最后一条数据: ``` SELECT person, MAX(submit_time) FROM table_name GROUP BY person ``` 在这个查询,首先使用 `GROUP BY` 语句对数据按人员分组。然后,使用 `MAX` 函数找到每组的最大,即每个人员提交的最后一条数据。最后,选择每个人员和他们的最后一条提交时间。 ### 回答2: SQL查询每个人员所提交的最后一条数据可以通过使用子查询和MAX函数来实现。 可以使用如下的SQL语句来达到目的: ``` SELECT p.name, s.data FROM person p JOIN ( SELECT person_id, MAX(submit_date) AS max_date FROM submission GROUP BY person_id ) m ON p.id = m.person_id JOIN submission s ON p.id = s.person_id AND s.submit_date = m.max_date ``` 假设数据库有两个表:`person`(包含人员信息)和`submission`(包含人员提交的数据信息)。在以上查询,首先使用子查询获取每个人员的最大提交日期,然后与`person`表和`submission`表进行连接,获取每个人员最后一条数据的相关信息。最终结果将返回每个人员的姓名和他们提交的最后一条数据的内容。 需要注意的是,以上查询假设`person`表的主键为`id`,`submission`表与`person`表关联的外键为`person_id`,并且`submission`表记录人员的提交日期为`submit_date`。根据实际情况,可能需要调整表名和列名。 ### 回答3: SQL查询每个人员提交的最后一条数据可以通过以下步骤进行: 首先,创建一个名为"employees"的表,表包含以下字段: - employee_id: 人员ID,唯一标识每个人员 - submit_date: 提交日期,表示数据提交时间的字段 - data: 提交的数据内容 然后,使用以下SQL语句查询每个人员提交的最后一条数据: ``` SELECT e.employee_id, e.submit_date, e.data FROM employees e INNER JOIN ( SELECT employee_id, MAX(submit_date) AS last_submit_date FROM employees GROUP BY employee_id ) subquery ON e.employee_id = subquery.employee_id AND e.submit_date = subquery.last_submit_date; ``` 以上SQL语句的步骤解释如下: 1. 使用子查询得到每个员工ID的最后提交日期,这是通过在"employees"表使用GROUP BY子句和MAX聚合函数计算得到。 2. 将子查询作为内连接的一部分,将每个人员的ID和最后提交日期与整个"employees"表匹配。 3. 最后,从结果选择所需的字段,包括员工ID、提交日期和数据内容。 执行以上SQL语句后,将返回包含每个人员最后一条提交数据的结果集。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值