1

我有一个mysql表,每行有一个学期的注册号、出生日期、学生姓名、性别、总分。我想在单个 SQL 查询中按总分的降序获取 10 个男孩和 5 个女孩的记录。

4

3 回答 3

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)

请注意,这是一个查询。

于 2012-07-31T13:20:11.437 回答
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
于 2012-07-31T13:20:31.363 回答
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
于 2012-07-31T13:20:39.030 回答