有我有兩個表:從聲明基於表的名字從另一個表
table1
-id 1
-name animals
animals
-id 1
-age 13
現在我想創建SQL語句是這樣的:
select age from (select name from table1 where id = 1)
它是更多鈔票在做這個MS SQL?
Regards
有我有兩個表:從聲明基於表的名字從另一個表
table1
-id 1
-name animals
animals
-id 1
-age 13
現在我想創建SQL語句是這樣的:
select age from (select name from table1 where id = 1)
它是更多鈔票在做這個MS SQL?
Regards
我認爲這是一個糟糕的設計。我會使用一個關鍵扳平只有兩個表一起:
分類
ID Type
1 Animal
2 Person
3 Building
事情
ID Type Name Age
1 Animal Fluffy 13
2 Person Joe 23
3 Animal Lucy 3
4 Building Empire State Building 80
的查詢將是:
select age
from categories c
inner join things t on c.Type = t.Type
where c.ID = 1
添加一個ind在FK(加入)列前東西使這個快。
它只能使用動態sql。
declare @sql nvarchar(max)
select @sql = 'select age from ' + name from table1 where id=1
exec sp_executesql @sql
注意,這不是一般的好主意,你會好得多改變你的設計爲每tvanfosson's answer。
你想要什麼,我認爲:
SELECT T1.NAME, T2.AGE
FROM TABLE1 T1
INNER JOIN ANIMALS T2 ON T1.ID=T2.ID
WHERE ID=1
+1,我同意這是一個不好的設計 – 2011-03-11 11:57:54