在mybatis中mapper.xml中如何实现sql动态条件查询结果后求和
现在遇到个状况,如下代码selectrela.member_paper_id,rela.member_id,paper_type,rela.paper_id,rela.c...
现在遇到个状况,如下代码
select
rela.member_paper_id,rela.member_id,paper_type,rela.paper_id,rela.create_time,rela.update_time,rela.start_time,rela.end_time,me.member_name,pa.paper_name,concat (rela.paper_score ,'分' ) as paper_score,concat (pa.total_score ,'分' ) as total_score,
cast((
select count(1) from
training_member_paper_question_relation qrela
left join training_member_paper_relation mpr on qrela.member_paper_id = mpr.member_paper_id
where
qrela.member_paper_id = rela.member_paper_id and is_correct = '0'
) as char(5))as error_number,
cast((
select
SUM(d.question_score)
from
(select qrela.question_score from training_member_paper_question_relation qrela
left join training_paper_question que on qrela.question_id = que.question_id
where qrela.member_paper_id = '2017110900011225' and (que.question_type = 'single' or que.question_type = 'order' or que.question_type = 'multiple')
)as d
) as char(5)) as subjective_score,
cast((
select
COUNT(1)
from training_member_paper_question_relation qrela
left join training_paper_question que on qrela.question_id = que.question_id
where qrela.member_paper_id = rela.member_paper_id and (que.question_type = 'single' or que.question_type = 'order' or que.question_type = 'multiple')
) as char(5))as subjective_item
from
training_member_paper_relation rela
left join training_member me on rela.member_id = me.member_id
left join training_paper pa on rela.paper_id = pa.paper_id
WHERE rela.member_paper_id = '2017110900011225'
在这种情况下是可以查出member_paper_id = '2017110900011225'的数据,这个没问题,但我的WHERE条件后的rela.member_paper_id 在mapper是动态的(= #{memberPaperId,jdbcType=CHAR}) ,那么在上面 qrela.member_paper_id 就不可能为= '2017110900011225'不变的,但是我把它改成像下面一个字段的方式: qrela.member_paper_id = rela.member_paper_id 直接报错[Err] 1054 - Unknown column 'rela.member_paper_id' in 'where clause',
subjective_score字段的查询是先查出条件下的结果,再把结果求和,再把这个作为一个字段去查询,下面一个 subjective_item字段查询是直接查出数量,再作为一个字段查询,这个是没有问题的,请问大神这个有没有什么办法啊 展开
select
rela.member_paper_id,rela.member_id,paper_type,rela.paper_id,rela.create_time,rela.update_time,rela.start_time,rela.end_time,me.member_name,pa.paper_name,concat (rela.paper_score ,'分' ) as paper_score,concat (pa.total_score ,'分' ) as total_score,
cast((
select count(1) from
training_member_paper_question_relation qrela
left join training_member_paper_relation mpr on qrela.member_paper_id = mpr.member_paper_id
where
qrela.member_paper_id = rela.member_paper_id and is_correct = '0'
) as char(5))as error_number,
cast((
select
SUM(d.question_score)
from
(select qrela.question_score from training_member_paper_question_relation qrela
left join training_paper_question que on qrela.question_id = que.question_id
where qrela.member_paper_id = '2017110900011225' and (que.question_type = 'single' or que.question_type = 'order' or que.question_type = 'multiple')
)as d
) as char(5)) as subjective_score,
cast((
select
COUNT(1)
from training_member_paper_question_relation qrela
left join training_paper_question que on qrela.question_id = que.question_id
where qrela.member_paper_id = rela.member_paper_id and (que.question_type = 'single' or que.question_type = 'order' or que.question_type = 'multiple')
) as char(5))as subjective_item
from
training_member_paper_relation rela
left join training_member me on rela.member_id = me.member_id
left join training_paper pa on rela.paper_id = pa.paper_id
WHERE rela.member_paper_id = '2017110900011225'
在这种情况下是可以查出member_paper_id = '2017110900011225'的数据,这个没问题,但我的WHERE条件后的rela.member_paper_id 在mapper是动态的(= #{memberPaperId,jdbcType=CHAR}) ,那么在上面 qrela.member_paper_id 就不可能为= '2017110900011225'不变的,但是我把它改成像下面一个字段的方式: qrela.member_paper_id = rela.member_paper_id 直接报错[Err] 1054 - Unknown column 'rela.member_paper_id' in 'where clause',
subjective_score字段的查询是先查出条件下的结果,再把结果求和,再把这个作为一个字段去查询,下面一个 subjective_item字段查询是直接查出数量,再作为一个字段查询,这个是没有问题的,请问大神这个有没有什么办法啊 展开
展开全部
mybatis配置可以在任何时候打印SQL ,应该把所有的配置都改为DEBUG模式。
# This is the configuring for logging displayed in the Application Server
log4j.rootCategory=DEBUG,stdout,logfile
log4j.logger.java.sql.PreparedStatement=debug,stdout
log4j.logger.java.sql.ResultSet=debug
#stdout configure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n
# This is the configuring for logging displayed in the Application Server
log4j.rootCategory=DEBUG,stdout,logfile
log4j.logger.java.sql.PreparedStatement=debug,stdout
log4j.logger.java.sql.ResultSet=debug
#stdout configure
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询