2015-06-10 42 views
1

我需要一个包含3个表格和2个内部连接的查询。一个在我的SELECT语句中列名的包括空间(现有的数据库),所以它看起来是这样的:带空格的列的别名?

SELECT tblCar.Purchase Price AS Price 

SQL不喜欢当然的空间。我尝试了括号[tblCar.Purchase Price],单引号和双引号"tblCar.Purchase Price",以及tblCar.[Purchase Price],但他们都没有飞。

任何想法如何在此SELECT语句中用空格处理列名称?

+1

[tblCar]。[采购价格]适用于MS SQL。 –

+0

它是SQL Server吗?哪个版本? – MarcusVinicius

+0

我做了以下[SqlFiddle](http://sqlfiddle.com/#!3/55173/1)和'tblCar。[Purchase Price]'正常工作。 – MarcusVinicius

回答

0

将表名和列名放在括号[tblCar] [采购价格]中。将表名引入MS SQL Server中不起作用。

2

各单位必须在一个单独的方括号,当你做这样的事情:

[tblCar.Purchase Price] 

据认为,这是完整的列名,而不是尝试这个[tblCar] [购买价格]。检查下面的链接了解详情:

How do you deal with blank spaces in column names in SQL Server?

sql server:what do brackets mean around column name?

How can I reference a column with a space in the name?

其实你并不需要为表名方括号,其仅适用于带空间列名,所以这也将工作:

tblCar.[Purchase Price] 
1

在你的情况,如果你使用SQ L SERVER,你可以这样做,将方括号中的名称包装起来。

它,但是,最好避免在名称中如果有可能的空间

SELECT [tblCar].[Purchase Price] AS Price 

而且如果有人在MySQL得到这个problam比这可以通过适当引用列名来解决:

SELECT `tblCar.Purchase Price` AS Price 
0

在SQL Server 2008 R2中,tblCar。[Purchase Price]适合我。你正在使用哪个版本的SQL版本?