有没有办法也 select 来自同一个查询的行数?

Is there a way to also select the row count from the same following query?

我可以向同一个查询添加行计数还是需要创建一个单独的行计数?

SELECT DISTINCT c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, \
                ( \
                    SELECT DISTINCT p.CONSTRAINT_TYPE \
                    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu \
                        INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS p ON p.CONSTRAINT_NAME=kcu.CONSTRAINT_NAME AND p.CONSTRAINT_TYPE=\'PRIMARY KEY\' \
                    WHERE kcu.TABLE_NAME = c.TABLE_NAME AND kcu.COLUMN_NAME=c.COLUMN_NAME \
                ) AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE \
                FROM INFORMATION_SCHEMA.COLUMNS c 

你可以用 cte 做到这一点

    with cte as
    (
    SELECT DISTINCT c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, 
                    ( 
                        SELECT DISTINCT p.CONSTRAINT_TYPE 
                        FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu 
                            INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS p ON p.CONSTRAINT_NAME=kcu.CONSTRAINT_NAME AND p.CONSTRAINT_TYPE='PRIMARY KEY' 
                        WHERE kcu.TABLE_NAME = c.TABLE_NAME AND kcu.COLUMN_NAME=c.COLUMN_NAME 
                    ) AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE 
                    FROM INFORMATION_SCHEMA.COLUMNS c 
    )
select *, (select count(*) from cte)  as  _Count
from
cte 

一般来说,如果不需要相关子查询并且您不支持 window functions,您应该避免 运行在 SELECT 中使用子查询,(在 MySQL 中可用8+).因此,将子查询作为派生的 table 移动到 JOIN 并简单地合并行计数 运行 JOIN INFORMATION_SCHEMA.TABLES:

SELECT DISTINCT c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, 
                t.CONSTRAINT_TYPE, NULL AS DATA_PRECISION, 
                NULL AS DATA_SCALE, r.TABLE_ROWS     

FROM INFORMATION_SCHEMA.COLUMNS c

INNER JOIN (
   SELECT DISTINCT kcu.TABLE_NAME, kcu.COLUMN_NAME, p.CONSTRAINT_TYPE
   FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
   INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS p
       ON p.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
      AND p.CONSTRAINT_TYPE = \'PRIMARY KEY\'
  ) AS t
  ON t.TABLE_NAME = c.TABLE_NAME 
  AND t.COLUMN_NAME = c.COLUMN_NAME

INNER JOIN INFORMATION_SCHEMA.TABLES r
  ON r.TABLE_NAME = c.TABLE_NAME 
  AND r.TABLE_SCHEMA = c.TABLE_SCHEMA
SELECT DISTINCT c.TABLE_SCHEMA, 
                c.TABLE_NAME, 
                c.COLUMN_NAME, 
                c.DATA_TYPE, 
                ( SELECT DISTINCT p.CONSTRAINT_TYPE 
                  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu 
                  INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS p 
                       ON p.CONSTRAINT_NAME=kcu.CONSTRAINT_NAME 
                      AND p.CONSTRAINT_TYPE='PRIMARY KEY' 
                  WHERE kcu.TABLE_NAME = c.TABLE_NAME 
                    AND kcu.COLUMN_NAME=c.COLUMN_NAME ) AS CONSTRAINT_TYPE, 
                NULL AS DATA_PRECISION, 
                NULL AS DATA_SCALE,

                COUNT(*) OVER () total_records_count 

FROM INFORMATION_SCHEMA.COLUMNS c