如何在一个查询中使用sql条件进行过滤?

时间:2019-04-18 11:20:25

标签: mysql

很难说我怎么了,但是我想在这里:

  SELECT name
       , (SELECT count(age) as count) 
    FROM 'students' 
   WHERE class = '1c' 
       ;

  SELECT name
       , (SELECT count(age) as count) 
    FROM 'students' 
   WHERE class = '2b'

它们是2个独立的mysql语句,所以我该如何计算一个语句 显示类似的内容:

+--------+-------+
| name   | count |
+--------+-------+
| Peeter |   5   |
| Harry  |   3   |
+--------+-------+

1 个答案:

答案 0 :(得分:1)

您可以在下面尝试-使用group byoperator

SELECT name,  count(age) as count
FROM 'students' 
WHERE class in ('1c','2b')
group by name