条件:假设有N张表,事先是不知道表结构的,要求的结果是 
结果一:要查询出这N张表中所有列名相同的列 
结果二:每一张表要返回它除去相同列之外的列 
请教这种要求应该如何写SQL语句? 
 
比如: 
如果A表里有customer_id,customer_name,book_id,book_amount 
B表里有company_id,company_name, book_id,book_amount 
那么要查出book_id和book_amount两列,我需要得到这个返回结果后显示在一个swing面板上的; 
而除了查出这相同列名的两列之外,我需要得到A表的去除了这两列之外的所有列,也就是举例中的customer_id,customer_name两列,我也需要显示在一个面板上; 
当然,B表的去除了这两列之外的所有列,也就是举例中的company_id,company_name,我也要把这个结果显示在一个面板上; 
以此类推,N张表,我需要得到N+1个返回结果,一个是所有相同列名的列(不考虑类型、精度等)的返回结果,还有每张表各自的去掉了这些相同列名外的列。 
说起来比较绕口,不知道各位高人能否了解

解决方案 »

  1.   

    补充一下,我用的是MySQL5系列版本的数据库
      

  2.   

    SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME
    ='YOUTABLENAME' 
      

  3.   

    大概这样的思路:显示相同列:
    select table_schema,table_name,column_name from information_schema.columns
    where table_schema='库名' and column_name in (select column_name from information_schema.columns where table_schema='库名' group by column_name having count(*)>1)
    显示不相同列:
    select table_schema,table_name,column_name from information_schema.columns
    where table_schema='库名' and column_name not in (select column_name from information_schema.columns where table_schema='库名' group by column_name having count(*)>1)
      

  4.   

    select distinct column_name from information_schema.columns where table_schema='数据库名' and table_name in('表一' ,'表二') and column_name in (select column_name from information_schema.columns where table_schema='数据库名' and table_name in('表一' ,'表二')  group by column_name having count(*)>1);我这样写查询出相同的列来,请指正
      

  5.   

    好像我的sql语句有问题,只要任意两张表之间有相同字段就显示出来,而不是所有表里都有这个字段才显示出来
      

  6.   


    按你上面的大概意思,则只需要这样即可:select column_name from information_schema.columns where table_schema='数据库名' and table_name in('表一' ,'表二')  group by column_name having count(*)>1
      

  7.   

    搞定了,having count(*)>1改成having count(*)=表数量就可以了,谢谢4楼