Sql Oracle查找具有特定列的所有表
我想查找数据库中有两列的所有表Sql Oracle查找具有特定列的所有表,sql,oracle,Sql,Oracle,我想查找数据库中有两列的所有表LOCATION和ASSET\u ID 所以我试了一下: select owner, table_name, column_name from all_tab_columns where column_name in ('LOCATION','ASSET_ID'); 问题是此查询提供了所有具有LOCATION或ASSET\u ID的表,而不是同时提供这两个表 所以我把它改成: select owner, table_name, column_name from a
LOCATION
和ASSET\u ID
所以我试了一下:
select owner, table_name, column_name
from all_tab_columns
where column_name in ('LOCATION','ASSET_ID');
问题是此查询提供了所有具有LOCATION
或ASSET\u ID
的表,而不是同时提供这两个表
所以我把它改成:
select owner, table_name, column_name
from all_tab_columns
where 1=1
and column_name ='LOCATION'
and column_name = 'ASSET_ID';
结果显示为0
请提供帮助。选择初始尝试中的所有行。然后按
owner
和table\u name
分组,只保留在初始查询中返回了两行的数据。请使用having
子句:
select owner, table_name
from all_tab_columns
where column_name in ('LOCATION','ASSET_ID')
group by owner, table_name
having count(*) = 2
;
您可以使用几个作为公共表表达式分解的子查询和联接来实现这一点,如中所示:
WITH cteLOCATION AS (SELECT OWNER, TABLE_NAME
FROM ALL_TAB_COLS
WHERE COLUMN_NAME = 'LOCATION'),
cteASSET_ID AS (SELECT OWNER, TABLE_NAME
FROM ALL_TAB_COLS
WHERE COLUMN_NAME = 'ASSET_ID')
SELECT OWNER, TABLE_NAME
FROM cteLOCATION
NATURAL JOIN cteASSET_ID
这是我使用自然连接
的极少数情况之一
祝你好运。试试这个
SELECT *
FROM (SELECT a.*,
COUNT(1) OVER (PARTITION BY table_name) cnt
FROM all_tab_columns a
WHERE column_name IN ('LOCATION','ASSET_ID')
ORDER BY table_name)
WHERE cnt = 2
使用联接(与@BobJarvis类似,但更为传统,没有with-子句):
或使用集合:
select owner, table_name
from all_tab_columns
where column_name='LOCATION'
intersect
select owner, table_name
from all_tab_columns
where column_name='ASSET_ID'
order by owner, table_name
或者使用@mathguy在此处发布的“groupby”子句
select owner, table_name
from all_tab_columns
where column_name in ('LOCATION', 'ASSET_ID')
group by owner, table_name
having count(*)=2
order by owner, table_name
当然,第一个给你OR,第二个没有给你答案-你期望什么?sql有一个“having”子句来避免这样的选择嵌套:
select owner, table_name
from all_tab_columns
where column_name in ('LOCATION', 'ASSET_ID')
group by owner, table_name
having count(*)=2
order by owner, table_name