是否可以计算项目以及在mySQL数据库查询中选择它的值?
SELECT a.id AS item_id,
b.id AS group_id,
c.user_id,
COUNT(c.user_id) AS cnt
答案 0 :(得分:2)
没有。
您需要使用嵌套查询:
SELECT
a.id AS item_id,
b.id AS group_id,
c.user_id,
d.cnt
FROM
tableA a,
tableB b,
tableC c,
(SELECT COUNT(user_id) AS cnt from tableC) as d --nested query
答案 1 :(得分:2)
当然可以。例如,它计算每个表名在information_schema.columns表中出现的次数,并显示它。
Select
count(table_name),
table_name
from
information_schema.columns
group by
table_name
显然Neal认为sql以不同的方式在information_schema表上工作(这对我来说是新闻)这是另一个例子
Create table ANomalTable
(
a_id int,
b_id int,
user_id int
);
INSERT INTO ANomalTable
VALUES (1,1,1);
INSERT INTO ANomalTable
VALUES (2,2,1);
INSERT INTO ANomalTable
VALUES (3,4,2);
INSERT INTO ANomalTable
VALUES (5,6,2);
SELECT
count(user_id),
user_id
FROm ANomalTable
GROUP BY
(user_id)
输出
count(user_id) user_id
------------- -------
2, 1
2, 2