发表说说数量最多的user

本文通过具体的SQL示例,介绍了如何从多个表中选取发表说说数量最多的用户,并探讨了将这些复杂的SQL查询转换为Hibernate HQL的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求发表说说数量最多的user  三张表 t_user t_userinfo t_talk.见名知意

 

先建一张表test,做个测试 

 

 

mysql> select * from test;
+------+------+
| id   | num  |
+------+------+
|   14 |    1   |
|   14 |    3   |
|   14 |    4   |
|   10 |    5   |
+------+------+

 

明知会得到多个结果 

mysql> select num from test where id=(select max(id) from test)

    -> ;
+------+
| num  |
+------+
|    1 |
|    3 |
|    4 |
+------+

 加个limit,就得到一条结果

mysql> select num from test where id=(select max(id) from test) limit 1
    -> ;
+------+
| num  |
+------+
|    1 |
+------+

 得到t_talk里面发表说最多的人的uid

 

select  t2.publisher from 
	(select count(cont) contSize,publisher from t_talk group by publisher) t2 
 	where t2.contSize= 
 	(select max(t1.contSize) from (select count(cont) contSize from t_talk group by publisher) t1)
+-----------+
| publisher |
+-----------+
|         2 |
+-----------+

 到此为此,拿到publisher就可以用hibernate去拿Load了。但是t_talk里有t_user外键,如何用hql来描述上面这一段sql呢。我晕。

 

我描述错了,是取说说最多前几位用户。以下hql语句居然可以。但我把hibernate的东东用成这个样子,感觉到shamfull.

 

select u.uid,u.nikeName,i.imgUrl,count(*) from User u , Talk t,UserInfo i where t.publisher=u.uid	
 and i.uiid=u.uid  group by t.publisher order by count(*) desc

 

i.uiid中uiid不是entity类中的,居然是sql中的。why ?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值