MySQL Query计算来自多个表的所有记录

时间:2016-05-10 06:53:10

标签: mysql function count

我想要计算我尝试过的所有表中的所有记录,但这不起作用。

DELIMITER $
CREATE FUNCTION countr(p_tablename varchar(10)) RETURN int(10) BEGIN DECLARE ra int; DECLARE table_count INT;
SET ra=
  (SELECT COUNT(*) INTO table_count
   FROM information_schema.tables
   WHERE TABLE_SCHEMA = 'dest_northwind'
     AND TABLE_NAME = p_tablename); RETURN ra; END$ DELIMITER ;

1 个答案:

答案 0 :(得分:1)

这样做......

SELECT SUM(TABLE_ROWS) 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE();

更新 - 提问者希望只获取表子集的行,以便我更新它以包含各个表的总和或这些表子自己的计数。

表格行的总和......

SELECT SUM(TABLE_ROWS) 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME IN ('Table2','Table1');

按表格排列表格

SELECT TABLE_NAME,SUM(TABLE_ROWS) 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME IN ('Table2','Table1')
GROUP BY TABLE_NAME;