Home >Database >Mysql Tutorial >How to Count Rows from Multiple Tables in MySQL?

How to Count Rows from Multiple Tables in MySQL?

DDD
DDDOriginal
2024-11-03 20:05:29467browse

How to Count Rows from Multiple Tables in MySQL?

Counting Rows from Multiple Tables in MySQL

To retrieve the count of rows from multiple tables in MySQL, employ subqueries within a single query statement. For instance, to count rows from tables 'table1', 'table2', and 'table3' based on specific conditions, you can use the following syntax:

SELECT
  (SELECT COUNT(*) FROM table1 WHERE someCondition) AS table1Count,
  (SELECT COUNT(*) FROM table2 WHERE someCondition) AS table2Count,
  (SELECT COUNT(*) FROM table3 WHERE someCondition) AS table3Count

This query will return a result table with three columns: 'table1Count', 'table2Count', and 'table3Count'. Each column will display the count of rows satisfying the specified conditions. The output should resemble the format below:

+-------------+-------------+-------------+
| table1Count | table2Count | table3Count |
+-------------+-------------+-------------+
| 14          | 27          | 0           |
+-------------+-------------+-------------+

The above is the detailed content of How to Count Rows from Multiple Tables in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn