There are two queries as below:
有两个查询如下:
select column1, column2, column3, ....
from table1
where.... as T1
select count(*)
from table2
where column1 = T1.column1
Is it possible to combine both into one query
是否可以将两者结合到一个查询中
2 个解决方案
#1
1
Yes, we can, as an American president once said:
是的,我们可以像美国总统曾经说过的那样:
select table1.column1, count(*)
from table1
join table2
on table1.column1 = table2.column1
group by table1.column1
will tell you how many elements you have in each group
. If you need to select
other values as well, then you will either have to aggregate by them as well, by putting them into the group by
clause or use aggregate functions for them, like group_concat
to put the values into a list separated by your separator.
会告诉你每组中有多少元素。如果您还需要选择其他值,那么您也必须通过将它们放入group by子句或使用它们的聚合函数来聚合它们,例如group_concat将值放入由分隔符分隔的列表中。
#2
0
One simple way uses subqueries in the select
:
一种简单的方法是在select中使用子查询:
select (select column1, column2, column3, ....
from table1
where....
) as T1,
(select count(*)
from table2
where column1 = T1.column1
) as T2;
#1
1
Yes, we can, as an American president once said:
是的,我们可以像美国总统曾经说过的那样:
select table1.column1, count(*)
from table1
join table2
on table1.column1 = table2.column1
group by table1.column1
will tell you how many elements you have in each group
. If you need to select
other values as well, then you will either have to aggregate by them as well, by putting them into the group by
clause or use aggregate functions for them, like group_concat
to put the values into a list separated by your separator.
会告诉你每组中有多少元素。如果您还需要选择其他值,那么您也必须通过将它们放入group by子句或使用它们的聚合函数来聚合它们,例如group_concat将值放入由分隔符分隔的列表中。
#2
0
One simple way uses subqueries in the select
:
一种简单的方法是在select中使用子查询:
select (select column1, column2, column3, ....
from table1
where....
) as T1,
(select count(*)
from table2
where column1 = T1.column1
) as T2;