简体   繁体   中英

Single query to get all the table name, row count and count of rows where status is '1' in a given database

I am trying to get the table name, number of records in each table and number of records in each table with status='2' in a given database, and display the result through jtable plugin. I tried query something like

$sql = "SELECT TABLE_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' and TABLE_SCHEMA = '{$table_schema}' ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";";

TABLE_ROWS returns approx value, how to get exact value and how to get count of record with status=2 in same query.

Thanks, Samir

I think you will require to write a procedure which can return you custom row counts for each table. Below is query which can give you list of tables with total row count, but if you would to have row count based on filter(ie status=2) , than you will require to write a procedure where you can have a loop and get row counts for each table based on your filters.

  SELECT      SCHEMA_NAME(A.schema_id) + '.' +
            A.Name as tablename, SUM(B.rows) AS 'RowCount'
    FROM        sys.objects A
    INNER JOIN sys.partitions B ON A.object_id = B.object_id
    WHERE       A.type = 'U'
    GROUP BY    A.schema_id, A.Name

Thanks Suresh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM