【问题标题】:How to get table name and one column along with it in sql?如何在sql中获取表名和一列?
【发布时间】:2016-05-18 05:19:14
【问题描述】:

我的数据库中的表很少。我想获取所有表名以及一个字段,即 last_timestamp,这在每个表中都很常见。 我知道表名

我尝试了以下查询,但它只给了我一张表,我无法为我的要求编写查询

SELECT t.table_name, MAX(c.last_timestamp) FROM information_schema.tables t, city c WHERE t.table_name='city' or t.table_name  ='city_area' and TABLE_SCHEMA='my_db';

这给了我

'city', '2016-05-13 15:08:07',

我想我需要表格的动态别名。但我不明白如何去做。有可能吗?

【问题讨论】:

  • 你不能有动态的 FROM 部分。
  • 您对 mysql 或 sqlserver 的问题?还有什么你的确切问题?为什么要动态别名?

标签: mysql sql sql-server


【解决方案1】:

请尝试以下代码以获得所需的输出。

--Creating Two Temp tables
CREATE TABLE #Temp1
( 
ID int IDENTITY(1,1),
TableName Varchar(50)
)

CREATE TABLE #Temp2
( 
TableName Varchar(50),
last_timestamp Date
)
--Inserting the required tables into Temp table 1, From information_schema
INSERT INTO #Temp1
SELECT t.table_name AS TableName FROM information_schema.tables t WHERE    t.table_name IN ('Table1','Table2') AND TABLE_SCHEMA='my_db'
--Below code gives the desired output.
DECLARE @Count INT
DECLARE @Count1 INT
DECLARE @Query nvarchar(max)
DECLARE @TableVariable Varchar(100)
SET @Count1 =0
SET @Count =(SELECT count(ID) FROM #Temp1)
WHILE (@Count1<@Count) 
BEGIN
   SET @Count1 =(@Count1 +1)
   SET @TableVariable = (SELECT TableName FROM #Temp1 WHERE ID =@Count1)

   SET @Query = 'SELECT t.table_name AS TableName,max(c.last_timestamp)AS  last_timestamp FROM information_schema.tables t,' +@TableVariable+ ' c
              WHERE t.table_name IN ('''+@TableVariable+''') GROUP BY   t.table_name'
    INSERT  INTO #Temp2 EXECUTE SP_EXECUTESQL @Query
END
SELECT * FROM #Temp2

【讨论】:

    【解决方案2】:
    use AdventureWorks2012
    go
    declare @sql varchar(max) = ''
    declare @table varchar(max)
    declare sqlcursor cursor for
    SELECT  t.TABLE_SCHEMA + '.' + c.table_name
            --,c.column_name 
    from    [INFORMATION_SCHEMA].[TABLES] t
    join    [INFORMATION_SCHEMA].[COLUMNS] c on c.table_name = t.table_name
    WHERE   COLUMN_NAME = 'ModifiedDate'
    OPEN    sqlcursor   
    FETCH NEXT FROM sqlcursor INTO @table   
    set @sql = 'Select ' + char(39) + @table + char(39) + ', max(ModifiedDate) from '  + @table + ' '
    WHILE @@FETCH_STATUS = 0   
    BEGIN   
           FETCH NEXT FROM sqlcursor INTO @table 
           IF @@FETCH_STATUS = 0   
           set @sql = @sql + ' union ' +  'Select ' + char(39) + @table + char(39) + ', max(ModifiedDate) from '  + @table + ' '
    END   
    
    CLOSE sqlcursor   
    DEALLOCATE sqlcursor
    
    EXEC (@sql)
    

    【讨论】:

    • 与 Praveen 的回答类似,但不假设您知道表名。
    【解决方案3】:

    假设表是Q37290859Table1 和Q37290859Table2,每个表都有Id 和DateColumn 共同。然后,您可以使用简单的 while 循环(从而避免使用光标)来构建最终查询。

    declare @sql varchar(max)
    declare @table varchar(20)
    declare @column varchar(20)
    declare @tableList table 
    (
        table_id int identity(1,1), 
        table_name varchar(30), 
        column_name varchar(30)
    )
    
    insert into @tableList 
        select t.table_name, c.column_name 
        from information_schema.tables t
        inner join information_schema.columns c on t.table_name = c.table_name
        where c.column_name = 'DateColumn'
    
    declare @ctr int = 1
    declare @ctr_max int = (select max(table_id) from @tableList)
    set @sql = ''
    
    while @ctr <= @ctr_max
    begin
        select top 1 @table = table_name, @column = column_name from @tableList where table_id = @ctr
        if @ctr > 1 -- add union
            set @sql = @sql + ' union '
        set @sql = @sql + 'select '''+@table+''', max('+@column+') from ' + @table
        set @ctr = @ctr + 1
    end
    
    exec (@sql)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多