MySQL Error Code: 1248. Every derived table must have its own alias

在MySQL中执行SQL语句时,提示该错误(Error Code: 1248. Every derived table must have its own alias)。

这句话的意思是说每个派生出来的表都必须有一个自己的别名。嵌套查询的时候子查询出来的结果是作为一个派生表来进行上一级的查询的,所以子查询的结果必须要有一个别名。

执行的SQL语句为:SELECT COUNT(*) FROM (SELECT * FROM blacklist WHERE nId > 0 LIMIT 10)

把SQL语句改为:SELECT COUNT(*) FROM (SELECT * FROM blacklist WHERE nId > 0 LIMIT 10) AS CNT;

这样就可以了!

但实际上我出现这个错误的问题并不是上面的原因。花了许多时间发现的问题却是开发编程更改代码不细致造成的。生成后的SQL语句如下:

SELECT
	count( 0 ) 
FROM
	(
	SELECT
		sc.*,
		project.project_status AS project_status,
		a.enterprise_name AS owner_name,
		b.enterprise_name AS contractor_name,
		sum( pay.money ) AS return_fund_amount 
	FROM
		((((
						sale_contract sc
						JOIN project ON ((
								sc.sale_code = project.sale_code 
							)))
					JOIN payment_collection_situation pay ON ((
							sc.sale_code = pay.sale_code 
						)))
				JOIN enterprise a ON ((
						sc.owner_party_id = a.enterprise_code 
					)))
			JOIN enterprise b ON ((
					sc.contractor_party_id = b.enterprise_code 
				))) 
	WHERE
		1 = 1 
		AND b.enterprise_name LIKE concat( '%', '烦', '%' ) 
	) 
GROUP BY
	sc.sale_code,
	project.project_status 
ORDER BY
	project.project_status DESC,
	sc.retent_date ASC,
	sc.accept_date DESC 
	) tmpTable

  由于上面的SQL语句是由MyBatis生成的动态语句,经过分析发现是嵌套的SQL语句出现了语法错误,在WHERE子句部分有错误,多余了一个括号,而这个括号是在MyBatis的XML描述中编写错误造成的。主要还是由于编写代码修改不细致造成的。

posted @ 2022-10-24 10:36  培轩  阅读(663)  评论(0编辑  收藏  举报