我想查找数据库中包含两列LOCATION
和ASSET_ID
所以我尝试了这个:
select owner, table_name, column_name
from all_tab_columns
where column_name in ('LOCATION','ASSET_ID');
问题是此查询会为所有包含LOCATION
或ASSET_ID
的表格提供这两个表格。
所以我把它改成了这个:
select owner, table_name, column_name
from all_tab_columns
where 1=1
and column_name ='LOCATION'
and column_name = 'ASSET_ID';
它显示0结果。
请帮忙。
答案 0 :(得分:7)
选择初始尝试中的所有行。然后按owner
和table_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
;
答案 1 :(得分:0)
您可以使用一些子查询来做到这一点,这些子查询被考虑为公共表表达式和连接,如:
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
这是我使用NATURAL JOIN
的极少数情况之一。
祝你好运。
答案 2 :(得分:0)
使用连接(类似于@ BobJarvis' https://stackoverflow.com/a/47984132/754550但更传统而不带 - 子句):
select a1.owner,a1.table_name
from all_tab_columns a1,
all_tab_columns a2
where a1.owner=a2.owner
and a1.table_name=a2.table_name
and a1.column_name='LOCATION'
and a2.column_name='ASSET_ID'
order by a1.owner,a1.table_name
或使用集合:
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
或使用' group by' @mathguy
已在https://stackoverflow.com/a/47981174/754550张贴的条款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
答案 3 :(得分:-1)
试试这个,
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