【问题标题】:SQLCount with join query连接查询的 SQL 计数
【发布时间】:2017-10-02 13:30:37
【问题描述】:

我正在使用 angularjs。我正在尝试使用连接查询来显示计数。我有三个表。它的结构如下所示

筹款活动

    fundraiserId  
     1

筹款用户

    fundraisinguserId  userId fundraiserId
        1                  1         1

用户

        userId //and other columns 
           1

现在我正在编写类似这样的查询

SELECT f.fundraiserId 
     , fu.userId 
  FROM fundraisers f 
  LEFT 
  JOIN fundraisingusers fu 
    ON f.fundraiserId = fu.fundraiserId 
  LEFT 
  JOIN users u 
    ON fu.userId = u.userId;

现在在前面的 html 中,我想显示用户数,即fundraisingusers 表中存在的用户数。在这种情况下 1。但现在我不知道如何在联接查询中获取计数。目前我正在获取 userId .我需要将其显示为计数而不是 ID。

这是我的html

    <div class="col-xs-6 col-sm-4 col-md-4" data-ng-repeat="fundraise
    in fundraises">
    <span class="glyphicon glyphicon-thumbs-up"></span>Supporters
    <h6>{{fundraise.userId}}</h6>//Here i need count of users from fundraisingusers table but i am getting userId
    </div>

这是我用来显示数据的 POJO 类

    public class Fundraisers implements Serializable{
    private Integer fundraiserId;
    private Integer userId;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "fundraiserId")
    public Integer getFundraiserId() {
    return fundraiserId;
    }
    public void setFundraiserId(Integer fundraiserId) {
    this.fundraiserId = fundraiserId;
    }

    @Transient
    @JoinColumn(insertable=false,updatable=false)
    public Integer getUserId() {
    return userId;
    }
    public void setUserId(Integer userId) {
    this.userId = userId;
    }

这是我的数据库访问 dao 代码

    @Override
    public List<Fundraisers> getFundsByIndexWithStatusForFront() 
    {
      List<Fundraisers> fundRaisers = new ArrayList<Fundraisers>();
      Query query = 
      sessionFactory.getCurrentSession().createSQLQuery(FUNDRAISES);//Here i am using above query
    query.setResultTransformer(Transformers.aliasToBean(Fundraisers.class));
        fundRaisers = query.list();
        return fundRaisers;
     }

谁能告诉我如何获得 userId 而不是 userId 的计数?

【问题讨论】:

  • 绝对没有点外部加入您从中选择任何列的表。
  • 我还有其他专栏。但出于发布目的,我认为这些没有必要

标签: mysql hibernate


【解决方案1】:
SELECT f.fundraiserId as fundraiserId,COUNT(fu.userId) as userId 
FROM
fundraisers f 
left outer join fundraisingusers fu on
f.fundraiserId = fu.fundraiserId 
left outer join users u on fu.userId = u.userId";

【讨论】:

  • 我已经更新了我的问题。我正在使用 pojo 来显示数据。如果我在查询中使用 count 它会抛出错误作为参数类型不匹配
【解决方案2】:

尝试使用 GROUP BY 并使用 count():

SELECT f.fundraiserId as fundraiserId,
       CAST(count(fu.userId) AS CHAR) as userId 
FROM fundraisers as f 
     left outer join fundraisingusers as fu on f.fundraiserId =fu.fundraiserId 
     left outer join users as u on fu.userId = u.userId
group by f.fundraiserId;

【讨论】:

  • 我已经更新了我的问题。我正在使用 pojo 来显示数据。如果我在查询中使用 count 它会抛出错误作为参数类型不匹配
  • @Sachin HR 您可以将数据转换为字符串。我已经编辑了我的答案
  • 我需要在 pojo 类中将 userId 的类型更改为 String 吗?
  • 你的pojo类是什么类型,需要在哪里获取count值?
  • userId 是 pojo 中的整数
【解决方案3】:
 SELECT COUNT(fu.userId) as userId ,

此函数返回用户数。
正确的查询:

SELECT f.fundraiserId as fundraiserId,COUNT(fu.userId) as userId FROM
fundraisers as f left outer join fundraisingusers as fu on
f.fundraiserId = fu.fundraiserId left outer join users as u on
fu.userId = u.userId";

【讨论】:

  • 我已经更新了我的问题。我正在使用 pojo 来显示数据。如果我在查询中使用 count 它会抛出错误作为参数类型不匹配
  • 你 POJO 是错误的,因为你必须创建一个与用户实体的多对多关系,因为将获得用户列表,这样你就可以计算这个列表并获取用户数量,是u 使用休眠?
  • 是的。我正在使用休眠
  • 在这种情况下,您可以使用“fundraisingusers”配置“fundraisers”和“users”之间的多对多关系,从FUNDRAISERS我们得到一个Fundraisingusers列表,里面有一个用户对象,我这样我们不能列出用户 id 不为空的列表,这里有一个链接来做这个配置,---mkyong.com/hibernate/…
猜你喜欢
  • 2014-03-01
  • 2023-04-09
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
相关资源
最近更新 更多