2011-12-03 23 views
0

我有多個具有相同列名稱的表(8)。每張桌子都是一個網站。我希望能夠搜索查看哪個table.column具有特定的值。即。 table.subnet = '10 .3.30.x'。提前致謝。搜索多個tables.column以獲取特定值

+0

還在嗎?很高興知道我們的答案是否有幫助! – codeling

回答

1

查看UNION ALL關鍵字。我猜你想要做的是這樣的:

(SELECT 'table1', table1.* FROM table1 where subnet = '10.3.30.x') 
UNION ALL 
(SELECT 'table2', table2.* FROM table2 where subnet = '10.3.30.x') 
UNION ALL 
... 
(SELECT 'table7', table7.* FROM table7 where subnet = '10.3.30.x') 
UNION ALL 
(SELECT 'table8', table8.* FROM table8 where subnet = '10.3.30.x') 

這樣一來,你的結果將有所有列在子網值是規定值,與該行出現在表的名稱一起

1

可以使用UNION得到所有值在一個查詢:

SELECT 'table1', subnet FROM table1 WHERE subnet = '10.3.30.x' 
UNION 
SELECT 'table2', subnet FROM table2 WHERE subnet = '10.3.30.x' 
UNION 
SELECT 'table3', subnet FROM table3 WHERE subnet = '10.3.30.x' 
UNION 
SELECT 'table4', subnet FROM table4 WHERE subnet = '10.3.30.x';