I have these queries :
我有这些疑问:
SELECT COUNT(*) FROM t_table WHERE color = 'YELLOW';
SELECT COUNT(*) FROM t_table WHERE color = 'BLUE';
SELECT COUNT(*) FROM t_table WHERE color = 'RED';
Is there any way to get these results in one query?
有没有办法在一个查询中获得这些结果?
6 个解决方案
#1
45
SELECT color, COUNT(*) FROM t_table GROUP BY color
#2
33
If you want the result to be in one row you can use:
如果您希望结果在一行中,您可以使用:
SELECT
SUM(IF(color = 'YELLOW', 1, 0)) AS YELLOW,
SUM(IF(color = 'BLUE', 1, 0)) AS BLUE,
SUM(IF(color = 'RED', 1, 0)) AS RED
FROM t_table
工作实例
#3
8
SELECT 'yellow' as color ,COUNT(*) FROM t_table WHERE color = 'YELLOW'
union
SELECT 'blue' , COUNT(*) FROM t_table WHERE color = 'BLUE'
union
SELECT 'red',COUNT(*) FROM t_table WHERE color = 'RED';
or
要么
select color, count(*) from table where color in ('red', 'blue', 'yellow') group by 1
#4
1
You can do this using subquery.
您可以使用子查询执行此操作。
SELECT(
SELECT COUNT(*) FROM t_table WHERE color = 'YELLOW',
SELECT COUNT(*) FROM t_table WHERE color = 'BLUE',
SELECT COUNT(*) FROM t_table WHERE color = 'RED'
);
#5
0
This is my answer: Este Ejemplo SQL Indica la cantidad de un Grupo y Suma los encontrado con S y N por separado. Se que no es la Respuesta pero puede ser usado para otros casos. Bendito sea Israel.
这是我的答案:Este Ejemplo SQL Indica la cantidad de un Grupo y Suma los encontrado con S y N por separado。 Se que no es la Respuesta pero puede ser usado para otros casos。本迪托海以色列。
SELECT sm_med_t_servicios.id as identidad, count(sm_adm_t_admision.id) as cantidad ,
SUM(IF(sm_adm_t_admision.atendido = 'S', 1, 0)) AS atendidos,
SUM(IF(sm_adm_t_admision.atendido = 'N', 1, 0)) AS por_ver
FROM sm_med_t_servicios
LEFT JOIN sm_adm_t_admision ON sm_med_t_servicios.id = sm_adm_t_admision.sm_med_t_servicios_id
WHERE sm_med_t_servicios.m_empresas_id = '2'
GROUP BY sm_med_t_servicios.id
I hope this helps you.
我希望这可以帮助你。
#6
0
I think this can also works for you
我认为这也适合你
select count(*) as anc,(select count(*) from Patient where sex='F')as
patientF,(select count(*) from Patient where sex='M') as patientM from anc
you can also even select and count related tables like this
你甚至可以像这样选择和统计相关的表格
select count(*) as anc,(select count(*) from Patient where
Patient.Id=anc.PatientId)as patientF,(select count(*) from Patient where
sex='M') as patientM from anc
#1
45
SELECT color, COUNT(*) FROM t_table GROUP BY color
#2
33
If you want the result to be in one row you can use:
如果您希望结果在一行中,您可以使用:
SELECT
SUM(IF(color = 'YELLOW', 1, 0)) AS YELLOW,
SUM(IF(color = 'BLUE', 1, 0)) AS BLUE,
SUM(IF(color = 'RED', 1, 0)) AS RED
FROM t_table
工作实例
#3
8
SELECT 'yellow' as color ,COUNT(*) FROM t_table WHERE color = 'YELLOW'
union
SELECT 'blue' , COUNT(*) FROM t_table WHERE color = 'BLUE'
union
SELECT 'red',COUNT(*) FROM t_table WHERE color = 'RED';
or
要么
select color, count(*) from table where color in ('red', 'blue', 'yellow') group by 1
#4
1
You can do this using subquery.
您可以使用子查询执行此操作。
SELECT(
SELECT COUNT(*) FROM t_table WHERE color = 'YELLOW',
SELECT COUNT(*) FROM t_table WHERE color = 'BLUE',
SELECT COUNT(*) FROM t_table WHERE color = 'RED'
);
#5
0
This is my answer: Este Ejemplo SQL Indica la cantidad de un Grupo y Suma los encontrado con S y N por separado. Se que no es la Respuesta pero puede ser usado para otros casos. Bendito sea Israel.
这是我的答案:Este Ejemplo SQL Indica la cantidad de un Grupo y Suma los encontrado con S y N por separado。 Se que no es la Respuesta pero puede ser usado para otros casos。本迪托海以色列。
SELECT sm_med_t_servicios.id as identidad, count(sm_adm_t_admision.id) as cantidad ,
SUM(IF(sm_adm_t_admision.atendido = 'S', 1, 0)) AS atendidos,
SUM(IF(sm_adm_t_admision.atendido = 'N', 1, 0)) AS por_ver
FROM sm_med_t_servicios
LEFT JOIN sm_adm_t_admision ON sm_med_t_servicios.id = sm_adm_t_admision.sm_med_t_servicios_id
WHERE sm_med_t_servicios.m_empresas_id = '2'
GROUP BY sm_med_t_servicios.id
I hope this helps you.
我希望这可以帮助你。
#6
0
I think this can also works for you
我认为这也适合你
select count(*) as anc,(select count(*) from Patient where sex='F')as
patientF,(select count(*) from Patient where sex='M') as patientM from anc
you can also even select and count related tables like this
你甚至可以像这样选择和统计相关的表格
select count(*) as anc,(select count(*) from Patient where
Patient.Id=anc.PatientId)as patientF,(select count(*) from Patient where
sex='M') as patientM from anc