常用的一些HQL查询语句详解(3)

来源:互联网 发布:蚁群优化算法的特点 编辑:程序博客网 时间:2024/06/05 20:34

(1)from Topic t where t.msgs is not empty

详解:empty:意思是空的,not empty:非空的  意思是取出 msgs不是空值的Topic内容;

(2)from Topic t where t.title like '%5'

详解:%代表查询多个(模糊查询),查询title中包含5的所有内容;

(3)from Topic t where t.title like '_5'

详解:精确查询5。"_"表示唯一的。

(4)select lower(t.title)," +  "upper(t.title)," +  "trim(t.title)," +  "concat(t.title, '***')," +  "length(t.title)" +  " from Topic t ");

详解:lower: 返回根据当前字符集映射所有字符改变为小写,即返回小写的字符串。
    upper:返回字符串str,根据当前字符集映射的所有字符更改为大写。
    trim:用来移除掉一个字串中的字头或字尾。最常见的用途是移除字首或字尾的空白
    concat:返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。(较复杂)
    length:返回的字符串str的长度,字符计算。多字节字符算作一个单独的字符。

(5)select abs(t.id)," + "sqrt(t.id)," +  "mod(t.id, 2)" + " from Topic t

详解:abs:返回绝对值  sqrt:返回非负平方根  mod:返回除以2之后的余数

(6)select current_date, current_time, current_timestamp from Topic t

详解:current_date :显示查询的日子、current_time:显示查询的时间、current_timestamp:显示查询的日期和时间

(7)Query q = session.createQuery("from  Topic t where t.createDate < :date");--q.setParameter("date", new Date());

详解:t.createDate 表中数据录入的时间 、date:自定义的时间

(8)select t.title, count(*) from Topic t group by t.title

详解:count(*):取得表中的记录总数;count是计算行数的函数,查询返回几行数据就显示几, 如果查询得到null值的话则不计数

            group by : group by是分组查询,一般与聚合函数配合使用(详解请看相关文章)

(9)select t.title, count(*) from Topic t group by t.title having count(*) >= 1

详解:查询所有返回数据行数大于等于1的数据

(10)from Topic t where t.id < (select avg(t.id) from Topic t)

详解:查询出id小于 id的平均数的数据

(11)from Topic t where t.id < ALL (select t.id from Topic t where mod(t.id, 2)= 0)

详解:查询 小于 所有 t.id中的偶数(即小于其中最小的那一个)

(12)from Topic t where not exists (select m.id from Msg m where m.topic.id=t.id)

详解:exists判断()里面的值是否存在存在 存在代表true 不存在代表false 。not 相反过来。另外,用in 可以实现exists的功能,但是exists执行效率更高


0 0