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

How to Count Rows from Multiple Tables in MySQL?

DDD
Release: 2024-11-03 20:05:29
Original
488 people have browsed it

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
Copy after login

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           |
+-------------+-------------+-------------+
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template