【问题标题】:Sybase execute query with multiple tables using jdbcSybase 使用 jdbc 对多个表执行查询
【发布时间】:2015-08-21 15:07:31
【问题描述】:

我们有一个要求,我们需要在多个表(超过 20 个)中搜索匹配条件的记录。 它是一个 Sybase 数据库,我们使用 JDBC 执行查询。

我们需要形成一个单一的查询,以便在一个单一的查询执行中我们从多个表中获取所有需要的数据,并且我们不需要对所有表执行多次查询。

这些表之间没有关系。它们只是日志表。

例子:

表格:table1、table2、table3、table4

需要得到类似下面的数据

select column1,column2,column3,column4 
from table1, table2, table3. table4 
where (table1.column1 or table2.column1 or table3.column1 or table4.column1) in (1,2,3,4)

因此,无论我们在哪个表中找到与 (1,2,3,4) 匹配的 id 列,都需要获取。记录只会出现在许多表中的任何一个中。

【问题讨论】:

    标签: sql database jdbc sybase


    【解决方案1】:

    使用union(或union all,如果你想接受重复)

    select t1.column1, t1.column2, t1.column3, t1.column4 from table1 t1 
        where t1.column1 in (1, 2, 3, 4)
    union
    select t2.column1, t2.column2, t2.column3, t2.column4 from table2 t2 
        where t2.column1 in (1, 2, 3, 4)
    union
    ...
    

    【讨论】:

    • 感谢回复我尝试在下面的查询中执行 select id from (select id from table1 union select id from table2 union select id from table3 union select id from table4) as table where table.id in (1, 2,3,4) 和它的工作正常。但是这里有一个疑问:是先从table1、table2、table3和table4中获取所有记录,然后在where子句中搜索id,还是根据where子句中指定的id从table1、table2、table3和table4中获取记录。因为如果它从所有表中获取所有记录,然后搜索where子句条件,它会影响内存。请提出建议。
    • 我想它会做正确的事。使用解释查询来找出答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多