我在QueryDSL中有以下查询:
Expression<?>[] projection = {
table.field1.as("field1"),
table.field2.as("field2")
};
SQLSubQuery subquery = new SQLSubQuery()
.from(table)
.where(...);
NumberPath<BigDecimal> field1 = new NumberPath<BigDecimal>(BigDecimal.class, "field1");
DateTimePath<Timestamp> field2 = new DateTimePath<Timestamp>(Timestamp.class, "field2");
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection))
.where(table.field1.eq(field1).and(table.field2.eq(field2)))
.groupBy(contentcache1.programId, contentcache1.id);
我正在尝试在主查询的子查询(字段 1 和字段 2)中引用别名字段。不幸的是,我得到这个错误:
java.lang.IllegalArgumentException: Undeclared path 'field1'. Add this path as a source to the query to be able to reference it.
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:65)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.path.NumberPath.accept(NumberPath.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:119)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.expr.BooleanOperation.accept(BooleanOperation.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:119)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.expr.BooleanOperation.accept(BooleanOperation.java:53)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:87)
at com.mysema.query.types.ValidatingVisitor.visit(ValidatingVisitor.java:27)
at com.mysema.query.types.query.ListSubQuery.accept(ListSubQuery.java:69)
at com.mysema.query.DefaultQueryMetadata.validate(DefaultQueryMetadata.java:318)
at com.mysema.query.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:112)
at com.mysema.query.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:102)
at com.mysema.query.support.QueryMixin.from(QueryMixin.java:143)
at com.mysema.query.jpa.AbstractSQLQuery.from(AbstractSQLQuery.java:67)
at com.irtve.plataforma.api.service.dsl.impl.AssetManagerImpl.findAllFirstInProgram(AssetManagerImpl.java:466)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy51.findAllFirstInProgram(Unknown Source)
at com.irtve.plataforma.bms.model.multimedia.video.VideoRepositoryImpl.findAllFirstInProgram(VideoRepositoryImpl.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy83.findAllFirstInProgram(Unknown Source)
at com.irtve.plataforma.bms.model.multimedia.video.VideosProgramasServiceImpl.findAllFirstInProgram(VideosProgramasServiceImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy105.findAllFirstInProgram(Unknown Source)
at com.irtve.plataforma.rest.service.multimedias.videosprogramas.delegate.VideosProgramasServiceDelegate.findAll(VideosProgramasServiceDelegate.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.cache.interceptor.CacheInterceptor$1.invoke(CacheInterceptor.java:58)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:213)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy195.findAll(Unknown Source)
at com.irtve.plataforma.rest.service.multimedias.videosprogramas.VideosProgramasServiceFacadeImpl.findAll(VideosProgramasServiceFacadeImpl.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.cache.interceptor.CacheInterceptor$1.invoke(CacheInterceptor.java:58)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:213)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy196.findAll(Unknown Source)
at com.irtve.plataforma.rest.service.multimedias.videosprogramas.controller.VideosProgramasController.findAll(VideosProgramasController.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at net.sf.ehcache.constructs.web.filter.CachingFilter.buildPage(CachingFilter.java:302)
at net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilter.buildPage(SimpleCachingHeadersPageCachingFilter.java:120)
at net.sf.ehcache.constructs.web.filter.CachingFilter.buildPageInfo(CachingFilter.java:247)
at net.sf.ehcache.constructs.web.filter.CachingFilter.doFilter(CachingFilter.java:216)
at com.irtve.plataforma.rest.cache.RestCacheFilter.doFilter(RestCacheFilter.java:87)
at net.sf.ehcache.constructs.web.filter.Filter.doFilter(Filter.java:86)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:167)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at com.googlecode.psiprobe.Tomcat60AgentValve.invoke(Tomcat60AgentValve.java:30)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
我还尝试了以下内容:
SSTable aliasedTable = new SSTable("AliasedTable");
SQLSubQuery subquery = new SQLSubQuery()
.from(aliasedTable)
.where(...);
NumberPath<BigDecimal> field1 = new NumberPath<BigDecimal>(BigDecimal.class, "field1");
DateTimePath<Timestamp> field2 = new DateTimePath<Timestamp>(Timestamp.class, "field2");
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection))
.where(table.field1.eq(aliasedTable.field1).and(table.field2.eq(aliasedTable.field2)))
.groupBy(contentcache1.programId, contentcache1.id);
但我得到的是相同的错误,只是“AliasedTable”而不是“field1”。
如何从outter select中引用别名字段?
先谢谢你。
我最终使用表达式模板做到了这一点:
NumberExpression<BigDecimal> field1 = Expressions.numberTemplate(BigDecimal.class, "field1");
DateTimeExpression<Timestamp> field2 = DateTimeTemplate.create(Timestamp.class, "field2");
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection))
.where(table.field1.eq(field1).and(table.field2.eq(field2)))
.groupBy(contentcache1.programId, contentcache1.id);
更新
虽然我是这样做的,但蒂莫的解决方案更好、更干净。
这应该有效
SSTable table2 = new SSTable("table2");
SQLSubQuery subquery = new SQLSubQuery()
.from(table2)
.where(...);
SQLSubQuery query = new SQLSubQuery()
.from(table, subquery.list(projection).as(table2))
.where(table.field1.eq(table2.field1), table.field2.eq(table2.field2))
.groupBy(contentcache1.programId, contentcache1.id);
本文向大家介绍MySQL中使用表别名与字段别名的基本教程,包括了MySQL中使用表别名与字段别名的基本教程的使用技巧和注意事项,需要的朋友参考一下 MySQL 表别名(Alias) SQL 表别名 在 SQL 语句中,可以为表名称及字段(列)名称指定别名(Alias),别名是 SQL 标准语法,几乎所有的数据库系统都支持。通过关键字 AS 来指定。 表别名语法: 上述 SQL 执行后的效果,给人感
问题内容: 我懂了 “无效的列名daysdiff”。 Maxlogtm是日期时间字段。是让我发疯的小东西。 问题答案: 通常,您不能在子句中引用字段别名。(将其视为包括别名在内的全部内容,在该子句之后应用。) 但是,如其他答案所述,您可以强制将SQL视为在子句之前进行处理。通常使用圆括号来强制操作的逻辑顺序,或者使用通用表表达式(CTE)来完成此操作: 括号/子选择: 或参阅亚当的CTE版本答案。
问题内容: 我在Netezza工作,或者您知道的是Analytics(分析)的纯数据-nzsql,但是我认为这是一个ANSI SQL问题。这个问题是如此基本,我什至不知道如何搜索。 这将返回“ SMORK,PLONK,SPROING,SPROING”,也就是说,重用CLUNK别名可以很好地进行查询,但是源表中的列会覆盖PLONK别名。现在,如果我真的想要源表中的列,则可以编写SELECT SMOR
问题内容: 我想实现以下目标: 假设我用“ …”代替了一个冗长而复杂的 CASE 语句,那么在选择并使用别名时, 我不想重复 它。 请注意,这将返回多行,因此在我的情况下,语句外的/是不好的。 问题答案: 一种工作方式是使用子查询: 为了提高可读性,您还可以使用:
我有一个通用的Spring数据存储库接口,它扩展了,允许我自定义查询执行。我正在尝试扩展内置在默认存储库实现中的基本相等性测试,以便能够使用Spring Data REST执行其他查询操作。例如:
问题内容: 我正在使用Elasticsearch的索引别名在更完整的索引上创建受限视图,以支持旧版搜索应用程序。这很好。但我也想从返回的结果中排除某些敏感字段(它们包含电子邮件地址,因此我们希望不进行收集。) 这是我所拥有的: 这适用于查询(我看不到field_with_email),过滤条件也适用(我得到了受限的索引),但是我仍然在索引别名的查询结果中看到field_with_email。 这应