Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从两个表中获取相似的列名和计数_Java_Sql_Sql Server - Fatal编程技术网

Java 从两个表中获取相似的列名和计数

Java 从两个表中获取相似的列名和计数,java,sql,sql-server,Java,Sql,Sql Server,我的数据库中有多个表,我想检查并找出是否有两个给定的表具有相似的列名。为此,我创建了3个示例表,如下所示: table1(song_cast, song_name, song_size) table2(song_size, singer, movie_name, song_cast) table3(movie_name, singer, song_name, song_cast) 我期望的结果是 |table1 & table2 | song_cast,

我的数据库中有多个表,我想检查并找出是否有两个给定的表具有相似的列名。为此,我创建了3个示例表,如下所示:

    table1(song_cast, song_name, song_size)
    table2(song_size, singer, movie_name, song_cast)
    table3(movie_name, singer, song_name, song_cast)
我期望的结果是

    |table1 & table2 | song_cast, song_size | 2 #count of common columns
    |table2 & table3 | singer, song_cast, movie_name | 3
    |table1 & table3 | song_name, song_cast | 2
我将在javaNetBeans IDE 8.2中运行这段代码,所以我将在NetBeans中获得这段输出。

使用两种方法


我不确定你问了什么问题。@notyou在本例中,预期结果是他的问题。@Sami行与预期结果的顺序不同,除非我遗漏了你暗示的其他内容?太棒了!!这正是我想要的。非常感谢你的努力。@shadowfeind很乐意帮忙@SqlZim是的,在我看到这是命令之后删除了注释:
;with cte as (
select c.*
from information_schema.tables t
  inner join information_schema.columns c
    on t.table_schema = c.table_schema
   and t.table_name = c.table_name
where t.table_type='base table'
)
, match as (
select 
    tables = l.table_name + ' & ' + r.table_name
  , l.column_name
from cte l
  inner join cte r
    on l.column_name = r.column_name
   and l.table_name < r.table_name
)
select 
    tables
  , columns = stuff((
  select ', '+ column_name
  from match i
  where m.tables = i.tables
  for xml path (''), type).value('(./text())[1]','nvarchar(max)')
  ,1,2,'')
  , matches = count(*)
from match m
group by tables
+-----------------+-------------------------------+---------+
|     tables      |            columns            | matches |
+-----------------+-------------------------------+---------+
| table1 & table2 | song_cast, song_size          |       2 |
| table1 & table3 | song_cast, song_name          |       2 |
| table2 & table3 | singer, movie_name, song_cast |       3 |
+-----------------+-------------------------------+---------+