项目用的是SSH+EXT。
现在作分页。分页肯定要得到总记录数。一般是 select count(*) from 表名。
但是当查询条件稍微复杂一点后就不行了。
例如 A表有十条数据 B表有两条数据,且B中的两个id在A中都有相等的ID
select * from A LEFT join b on a.id=b.id
这样 查询结果就有11条数据。而select count(*) from A却还是10.
请问怎么才能通过传入的SQL语句进行高效率的count(*)操作QueryTranslatorImpl queryTranslator = new QueryTranslatorImpl(
hql, hql, Collections.EMPTY_MAP,
(SessionFactoryImplementor) session
.getSessionFactory());
queryTranslator.compile(Collections.EMPTY_MAP, false);
String tempSQL = queryTranslator.getSQLString();
System.out.println(tempSQL);
// 将对应转换完成的SQL语句 套入查询模版
String countSQL = "select count(*) from (" + tempSQL
+ ") tmp_count_t";
// 创建Query 对象
System.out.println(countSQL);
Query query = session.createSQLQuery(countSQL);这是通过传入的hql语句惊醒的count(*)操作。请问传入SQL语句的时候有没有类似的操作
现在作分页。分页肯定要得到总记录数。一般是 select count(*) from 表名。
但是当查询条件稍微复杂一点后就不行了。
例如 A表有十条数据 B表有两条数据,且B中的两个id在A中都有相等的ID
select * from A LEFT join b on a.id=b.id
这样 查询结果就有11条数据。而select count(*) from A却还是10.
请问怎么才能通过传入的SQL语句进行高效率的count(*)操作QueryTranslatorImpl queryTranslator = new QueryTranslatorImpl(
hql, hql, Collections.EMPTY_MAP,
(SessionFactoryImplementor) session
.getSessionFactory());
queryTranslator.compile(Collections.EMPTY_MAP, false);
String tempSQL = queryTranslator.getSQLString();
System.out.println(tempSQL);
// 将对应转换完成的SQL语句 套入查询模版
String countSQL = "select count(*) from (" + tempSQL
+ ") tmp_count_t";
// 创建Query 对象
System.out.println(countSQL);
Query query = session.createSQLQuery(countSQL);这是通过传入的hql语句惊醒的count(*)操作。请问传入SQL语句的时候有没有类似的操作
SELECT COUNT(*) FROM (SELECT * FROM A JOING B ....)c
我之前表达的不清楚。就是查询条件不同,怎样才能根据不同的SQL语句(即不同的查询条件)得到count(*)
你不会连select count(1) from TABLE A怎么取这个总数都不会吧?
单独查询A表获得总数用select count(1) from TABLE A
那么 A表和B表连接的时候呢?
A和C连接的时候呢?
A和B,C连接的时候呢?
我说了 查询条件不确定。拜托你好好看看我的问题行么?
我的意思你能根据不同的条件能 SELECT 出来结果集~~
那么COUNT 就 COUNT 这个结果集就好了
我select count(*) from (select a.id ,a.name, a.pwd left join b on a.id=b.id
)
这样的话效率只是相对高了一点 毕竟 后边还是查询了 字段的
看了你的左连接是这样的
select a.id ,a.name, a.pwd left join b on a.id=b.id
那你的COUNT 应该是
select count(a.id) left join b on a.id=b.id
这样才对
是你没理解我的办法,我叫你写二个函数分别取,肯定要传二条不同的SQL,一条是COUNT取总数,一条是取记录,只要你拼的时候拼二条就可以了
select a.id ,a.name left join b on a.id=b.id left join c on a.id=c.id
select a.id ,a.name left join b on a.id=b.id left join c on a.id=c.id left join d on a.id =d .id
就这三个情况,你拼一个count(*) 我看看