如下SQL语句:

SELECT * FROM 
( SELECT ID,URL FROM alone_action  WHERE STATE = '1' ) 

在Oracle中可以执行,但在MySQL下无法通过,错误如下:

查询:SELECT * FROM ( SELECT ID,URL FROM alone_action WHERE STATE = '1' ) LIMIT 0, 1000

错误代码: 1248
Every derived table must have its own alias

Every derived table must have its own alias 意思是说 每个派生出来的表都必须有一个自己的别名 , 因为进行嵌套查询的时候子查询出来的的结果是作为一个派生表来进行上一级的查询的,所以子查询的结果必须要有一个别名

把MySQL语句改为如下即可

SELECT * FROM 
( SELECT ID,URL FROM alone_action  WHERE STATE = '1' ) as t
        -------------------- 分割线 --------------------

作者出现上述错误的原因不是单纯执行SQL的问题,而是有个项目是Oracle的需要迁移到MySQL,数据库迁移成功之后 运行报错

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Every derived table must have its own alias

原因是因为Hibernate的dialect方言问题,将配置文件中Oracle的方言改为MySQL即可

<property name="dialect">
    org.hibernate.dialect.MySQLDialect
</property>

附:RDBMS 方言

Table Dialect
DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 9i/10g org.hibernate.dialect.Oracle9Dialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Ingres org.hibernate.dialect.IngresDialect
Progress org.hibernate.dialect.ProgressDialect
Mckoi SQL org.hibernate.dialect.MckoiDialect
Interbase org.hibernate.dialect.InterbaseDialect
Pointbase org.hibernate.dialect.PointbaseDialect
FrontBase org.hibernate.dialect.FrontbaseDialect
Firebird org.hibernate.dialect.FirebirdDialect


作者:itmyhome


本文转载:CSDN博客