在相同输入数据的where条件下查询单个table的所有列
Quering all columns of single table in where condition for same input data
如果我们想在单列上根据条件获取信息,我们这样做
SELECT * FROM contact WHERE firstName = 'james'
如果我们想在多个列上放置条件,我们会这样做
SELECT * FROM contact WHERE firstName = 'james' OR lastName = 'james' OR businessName = 'james'
但是如果我们有超过 50 列呢?
除了 WHERE Condition with OR Keyword 之外还有更好的方法吗?
该方法不应涉及写入所有列名。
在 MySql 中有一种方法可以做到这一点,如 shown here。
没办法不写所有的列名,
但是你可以使用 IN
条件来缩短写的时间:
SELECT *
FROM contact
WHERE 'james' in (firstName, lastName, businessName)
如果您想搜索所有 VARCHAR2 列,那么以下脚本应该会有所帮助:
set pages 0;
set lines 200
select case when rn = 1 and rn_desc = 1 then 'select * from '||table_name||' where '||column_name||' = ''james'';'
when rn = 1 then 'select * from '||table_name||' where '||column_name||' = ''james'''
when rn_desc = 1 then ' and '||column_name||' = ''james'';'
else ' and '||column_name||' = ''james'''
end sql_stmt
from (select table_name,
column_name,
column_id,
row_number() over (partition by table_name order by column_id) rn,
row_number() over (partition by table_name order by column_id desc) rn_desc
from user_tab_columns
where data_type in ('VARCHAR2')
-- and table_name in (<list of tables>) -- uncomment and amend as appropriate!
)
order by table_name, column_id;
如果您只想搜索特定的表,则必须为您要查找的 table_names 添加过滤器。
运行 以上作为脚本将为您提供一个包含多个查询的脚本,然后您可以 运行
如果我们想在单列上根据条件获取信息,我们这样做
SELECT * FROM contact WHERE firstName = 'james'
如果我们想在多个列上放置条件,我们会这样做
SELECT * FROM contact WHERE firstName = 'james' OR lastName = 'james' OR businessName = 'james'
但是如果我们有超过 50 列呢?
除了 WHERE Condition with OR Keyword 之外还有更好的方法吗? 该方法不应涉及写入所有列名。
在 MySql 中有一种方法可以做到这一点,如 shown here。
没办法不写所有的列名,
但是你可以使用 IN
条件来缩短写的时间:
SELECT *
FROM contact
WHERE 'james' in (firstName, lastName, businessName)
如果您想搜索所有 VARCHAR2 列,那么以下脚本应该会有所帮助:
set pages 0;
set lines 200
select case when rn = 1 and rn_desc = 1 then 'select * from '||table_name||' where '||column_name||' = ''james'';'
when rn = 1 then 'select * from '||table_name||' where '||column_name||' = ''james'''
when rn_desc = 1 then ' and '||column_name||' = ''james'';'
else ' and '||column_name||' = ''james'''
end sql_stmt
from (select table_name,
column_name,
column_id,
row_number() over (partition by table_name order by column_id) rn,
row_number() over (partition by table_name order by column_id desc) rn_desc
from user_tab_columns
where data_type in ('VARCHAR2')
-- and table_name in (<list of tables>) -- uncomment and amend as appropriate!
)
order by table_name, column_id;
如果您只想搜索特定的表,则必须为您要查找的 table_names 添加过滤器。
运行 以上作为脚本将为您提供一个包含多个查询的脚本,然后您可以 运行