使用查询的mysql结果

时间:2014-04-06 15:45:55

标签: mysql sql mysqli

我在MySQL中有两个2个字段(年龄,性别)的表 我需要做的是获得某种性别(m或f)的平均(年龄),有人可以帮我解决这个问题吗?一个查询应如何获得这两个看起来像?

2 个答案:

答案 0 :(得分:1)

select 
AVG(age) as AVG_AGE ,
sex
from test 
group by sex

点击http://sqlfiddle.com/#!2/fdc37/1

答案 1 :(得分:1)

试试这个:

   select 
 AVG(CASE WHEN sex = 'm' THEN age end) as AVG_male ,
 AVG(CASE WHEN sex = 'f' THEN age end) as AVG_female ,
 name,
 book_date
 from reviews r
 inner join books b
 on b.id = r.book_id
 where age<30        <-------------- do your where as you like
 group by book_id    <-------- you can group by here by what column you like.

DEMO HERE