Home > Database > Mysql Tutorial > body text

【ssh 分页问题】ssh开发,分页时mysql报duplicate column,求救

WBOY
Release: 2016-06-06 09:42:43
Original
1155 people have browsed it

分页mysqlduplicate column

ssh开发,数据库用的mysql。
分页时,需获取数据总函数totalCount,进一步得到totalPage,获取totalCount的代码(略去绑定参数、入参检查等代码):

<code> /** * 获取总记录数 */protected int getTotalCount(Session s, String sql){    //构建count查询    sql = "SELECT COUNT(1) FROM (" + sql + ") tmp";    Query countQuery = s.createSQLQuery(sql);    //输出结果    List countList = countQuery.list();    return (Integer)countList.get(0);}</code>
Copy after login

以上代码在特殊情况下报“Duplicate column name 'xxxx'”,例:
user表字段:**_user_id_**, login_name, passwd, ...
user_detail表字段:user_detail_id, user_id, address, ...
查询sql(**_代号SQL1_**):select * from user u inner join user_detail ud on u.user_id = ud.user_id

最终生成的查totalCount的sql(代号SQL2)是:
select count(1) from (
select * from user u inner join user_detail ud on u.user_id = ud.user_id
) tmp
执行报错,原因是user、user_detail表都有user_id字段,SQL1出来的结果包含了重名字段user_id,在SQL2中引用成tmp表时就会报重名错误。

想请教问题如下:
1、oracle中执行SQL2就不会报错,有没有办法改变mysql的设置使得执行SQL2的时候不再报错?
2、有没有其它的方法计算totalCount

小鲜肉写过大侠们了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template