Java教程

SQL查询规范1--单表嵌套查询需要给子查询赋予别买 as

本文主要是介绍SQL查询规范1--单表嵌套查询需要给子查询赋予别买 as,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

需求:

  1. 现有订单最早出账账期;

软件access;(测试MySQL也会报错)
单表查询,
需要嵌套子查询遇到了错误,

错误代码:子查询没有赋予别名,

-- 查询订单首次出现的账期(错误代码)
SELECT
	code1,
	month1,
	crm_bill_order_info.server_start_date 
FROM
	(
	SELECT
		crm_bill_order_info.order_code AS code1,
		Min( crm_bill_order_info.account_month ) AS month1 
	FROM
		crm_bill_order_info 
	WHERE
		crm_bill_order_info.customer = "客户名称" 
	GROUP BY
		crm_bill_order_info.order_code 
	)
	INNER JOIN crm_bill_order_info ON ( code1 = crm_bill_order_info.order_code ) 
	AND ( month1 = crm_bill_order_info.account_month );

得到错误提示:

1248 - Every derived table must have its own alias

修改:需要在子查询后赋予 “as [name]:as order_first_date”

-- 查询订单首次出现的账期
SELECT
	order_first_date.code1,
	order_first_date.month1,
	crm_bill_order_info.server_start_date 
FROM
	(
	SELECT
		crm_bill_order_info.order_code AS code1,
		Min( crm_bill_order_info.account_month ) AS month1 
	FROM
		crm_bill_order_info 
	WHERE
		crm_bill_order_info.customer = "客户名称" 
	GROUP BY
		crm_bill_order_info.order_code 
	) AS order_first_date
	INNER JOIN crm_bill_order_info ON ( order_first_date.code1 = crm_bill_order_info.order_code ) 
	AND ( order_first_date.month1 = crm_bill_order_info.account_month );

当遇到错误才能记忆的更清晰,才能提高。
入门的never_late_to_learn
不积跬步,无以至千里.不积小流,无以成江海–加油

这篇关于SQL查询规范1--单表嵌套查询需要给子查询赋予别买 as的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!