MySQL查询加入两个查询

时间:2012-07-31 13:15:26

标签: mysql

我有一个mysql表,每行一个学期都有注册号,dob,学生姓名,性别,总分。 我希望在单个SQL查询中按总分数的降序获得10个男孩和5个女孩的记录。

3 个答案:

答案 0 :(得分:1)

我的MySQL方言很生疏,但这应该可以解决问题

(SELECT * FROM Students WHERE sex = 'Male' ORDER BY TotalMarks DESC LIMIT 10)
UNION ALL
(SELECT * FROM Students WHERE sex = 'Female' ORDER BY TotalMarks DESC LIMIT 5)

这是一个查询,请注意。

答案 1 :(得分:0)

试试这段代码:

select 
    a.studentID,
    a.studentName,
    a.dob,
    a.totalMark
from
(
    select
        studentID,  
        studentName,
        dob,
        totalMark
    from
        students
    where
        sex='M'
    order by 
        studentMark desc
    limit 10
    union all
    select
        studentID,  
        studentName,
        dob,
        totalMark
    from
        students
    where
        sex='F'
    order by 
        totalMark desc
    limit 5
)
order by
    totalMark desc

答案 2 :(得分:0)

试试这个

    select * from
    (select reg_number, dob, student_name, sex, total_marks where sex='male' 
    order by total_marks desc limit 10
    union
    select reg_number, dob, student_name, sex, total_marks where sex='female'
    order by total_marks desc limit 5) a order by total_marks desc