如果我们想根据单列条件获取信息,我们会这样做
SELECT * FROM contact WHERE firstName = 'james'
如果我们想在多列上添加条件,我们就这样做
SELECT * FROM contact WHERE firstName = 'james' OR lastName = 'james' OR businessName = 'james'
但是,如果我们有超过50列,怎么办?
除了带有OR关键字的WHERE条件之外,还有更好的方法吗? 该方法不应涉及编写所有列名。
有一种方法可以在MySql中以shown here的方式执行此操作。
答案 0 :(得分:1)
您无法避免编写所有列名称,
但你可以使用IN
条件来缩短写作时间:
SELECT *
FROM contact
WHERE 'james' in (firstName, lastName, businessName)
答案 1 :(得分:1)
如果您想要搜索所有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设置一个过滤器。
将上述内容作为脚本运行将为您提供一个脚本,其中包含您可以运行的多个查询