gpt4 book ai didi

java - 如何从数据库中的多个表中获取公共(public)列?

转载 作者:可可西里 更新时间:2023-11-01 08:30:39 26 4
gpt4 key购买 nike

Greeting to all smart people around here !!

我遇到了一个关于 SQL 的奇怪面试问题。

Qn . 如果我在数据库中有 100 个表。我想从每个表中获取公共(public)记录。

例如,location是100张表中的公共(public)字段。我想从所有表中获取 location 字段,而不在我的 SQL 查询中提及每个表名。

有什么办法吗?

如果有任何可能性让我知道...

最佳答案

从数据库元数据中获取表列表,然后对每个表进行查询:

Statement stmt = conn.createStatement();
ResultSet locationRs = null;
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
locationRs = stmt.executeQuery("SELECT location from "+ rs.getString(3));
System.out.println(locationRs.getString(1));
}

关于java - 如何从数据库中的多个表中获取公共(public)列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27631657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com