2010-08-16 76 views
0

我有两个表test1 { id, name }test2 { id, name, family }和我写此查询:SQL查询列名

SELECT dbo.test1.*, dbo.test2.* 
FROM dbo.test1 
CROSS JOIN dbo.test2 

但在datagridview1,我想显示的字段(在头)是这样的:

test1.id test1.name test2.id test2.name test2.family

而是它们显示这样

id name id name family

我的查询需要哪些更改。

回答

1

你需要单独选择列,并使用as关键字:

SELECT dbo.test1.id as test1id, dbo.test2.id as test2id ... 
FROM dbo.test1 
CROSS JOIN dbo.test2 
+0

了'as'关键字并不是必需的,但可能使事情变得更具有可读性 – Scoregraphic 2010-08-16 08:46:58

+0

或者您可以使用方括号点我想,'dbo.test1.id为[test1.id]'。但是最好在数据网格控件中选择并重命名这些列。 – Rup 2010-08-16 08:47:16

+0

感谢您attention.if我有我想穿越加入他们更多的表,我想证明,像这样的Fileds每个tabale:tb1.filed1 tb1.filed2 tb2.filed1 .....我应该怎么办? – Farna 2010-08-16 09:15:58

3

简短的回答是:您可以随意更改网格列的标题。您也可以随意重新排列/隐藏/排序列。

3

我会写的选择,如:

select t1.id as "test1.Id", 
     t1.name as "test1.Name", 
     t2.id as "test2.Id", 
     t2.name as "test2.Name", 
     t2.family as "Test2.Family" 
from test1 t1, test2 t2 

但与查询,你会得到一个笛卡儿产品,如果你不添加适当的Where子句。

1

你在问如何对列进行别名?

SELECT 
    t1.id AS [test1.id], 
    t1.name AS [test1.name] , 
    t2.id AS [test2.id], 
    t2.name AS [test2.name] , 
    t2.family AS [test2.family] 
FROM dbo.test1 t1 
CROSS JOIN dbo.test2 t2 

由于您想要的名称不符合标识符的标准规则,因此需要引用它们。

1

坏码不好。不过,我会像@Scoregraphic提到的那样做。即使您的所有专栏回到co1; col1; col1,您都可以更改订单和标签。使用DataGridView中列的属性。