使用 MYSQL 在一个查询中选择两个表中的 COUNT

编程入门 行业动态 更新时间:2024-10-12 01:27:34
本文介绍了使用 MYSQL 在一个查询中选择两个表中的 COUNT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有一种方法可以通过一个查询选择带有两个表的 COUNT.

Is there a way i can select the COUNT with two table with one single query.

目前我有以下内容,但无法正常工作.

At the moment i have the following and it doesn't work correctly.

SELECT COUNT(t1.id) as t1_amount, COUNT(t2.id) as t2_amount FROM table1 t1, table2 t2

推荐答案

这是一种方法:

select (select count(*) from table1) as t1_amount, (select count(*) from table2) as t2_amount

这是另一种方式:

select t1.t1_amount, t2.t2_amount from (select count(*) as t1_amount from table1) t1 cross join (select count(*) as t2_amount from table2) t2

您的方法不起作用,因为 from 子句中的 , 执行了 cross join.这会在两个表之间进行笛卡尔积.

Your method does not work because the , in the from clause does a cross join. This does a cartesian product between the two tables.

更多推荐

使用 MYSQL 在一个查询中选择两个表中的 COUNT

本文发布于:2023-11-22 02:22:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   MYSQL   COUNT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!