加入多个信息/描述性数据库查询

时间:2018-02-06 20:32:20

标签: mysql join

我需要加入两个数据库结果,第一个:

int mapStringToIntWithCache(std::string const& s) {
    static std::unordered_map<std::string, int> cache;
    auto pair = cache.emplace( s, 0 );
    if( pair.second )
         pair.first->second = myExpensiveFunction(s);
    return pair.first->second;
}

它为我提供了包含数据类型的所有列,并且:

DESCRIBE my_table

为每个可能有一列的列提供 索引名称 。我需要在SHOW INDEXES FROM my_table 上加入它们,以便与DESCRIBE基本相同,但现在还有索引名称(如果它没有索引,则为show_indexes.Column_name = describe.Field)而不是{{1} }或NULL等。加入这两个结果就像使用两个PRI一样,似乎不起作用。

这在MySQL中甚至可能吗?

1 个答案:

答案 0 :(得分:0)

INFORMATION_SCHEMA

中检索它

1 - 显示等效的索引

SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = 'my_db' and TABLE_NAME = 'my_table';

2 - 描述等价物

SELECT COLUMN_NAME AS `Field`, COLUMN_TYPE AS `Type`, IS_NULLABLE AS `NULL`, 
   COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS `Extra` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'my_db' AND TABLE_NAME = 'my_table';

3 - 然后根据需要制定您的JOIN查询,这是一个例如:

    SELECT s.*, c.*
FROM
(
SELECT *
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'my_db' and TABLE_NAME = 'my_table'
) as s
RIGHT JOIN 
(
SELECT COLUMN_NAME AS `Field`, COLUMN_TYPE AS `Type`, IS_NULLABLE AS `NULL`, 
       COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS `Extra`
FROM information_schema.COLUMNS  
WHERE TABLE_SCHEMA = 'my_db' AND TABLE_NAME = 'my_table'
) as c
ON s.COLUMN_NAME = c.Field