为了获得顺序输出,我需要更改什么?

ttisahbt  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(224)

我是sql的新手。我被分配了一个任务,在这个任务中,我需要找出解决方案来获得顺序输出。
以下是查询:

use admin_database;

select rate , enrolledcount, is_request, is_wishlist, is_enroll, review_given_by_student, x.course_id, course_image_path, course_price, trainer_name, course_subtitle, course_title, max(batch_registration_end_date) as batch_registration_end_date, max(batch_start_date) as batch_start_date 
    from (
    SELECT
       (
        CASE WHEN noofStudents = 0 THEN 0 ELSE(
            ratings_by_enrolled_student /(5 * noofStudents)
        ) *100
    END
        ) AS rate,
    enrolledcount,
    is_request,
    is_wishlist,
    is_enroll,
    review_given_by_student,
    x.course_id,
    course_image_path,
    course_price,
    trainer_name,
    course_subtitle,
    course_title,
    batch_registration_end_date,
    batch_start_date
    FROM (
    SELECT
     COUNT( distinct e.enrolled_course_details_id) AS enrolledcount,

     SUM(
            CASE WHEN e.rating_by_enrolled_student <> 0 THEN 1 ELSE 0
        END
    ) AS noofStudents,
    SUM(
        CASE WHEN e.rating_by_enrolled_student <> 0 THEN e.rating_by_enrolled_student ELSE 0
    END
    ) AS ratings_by_enrolled_student,
    SUM(
        CASE WHEN LTRIM(RTRIM(e.review_given_by_student)) <> '' OR LTRIM(RTRIM(e.review_title)) <> '' THEN 1 ELSE 0
    END
    ) AS review_given_by_student,
        (SELECT count(s.user_id) FROM StudentEnrollRequest s WHERE s.user_id=".$user_id." AND accept_status='pending' AND s.course_id=c.course_id) AS is_request,
    (SELECT count(cw.user_id) FROM CourseWishlist cw WHERE cw.user_id=".$user_id." AND cw.course_id=c.course_id) AS is_wishlist,
    (SELECT count(eu.user_id) FROM EnrolledCourseDetails eu WHERE eu.user_id=".$user_id." AND eu.course_id=c.course_id) AS is_enroll,
    c.course_id AS course_id,
    c.course_image_path AS course_image_path,
    c.course_price AS course_price,
    c.trainer_name AS trainer_name,
    c.course_subtitle AS course_subtitle,
    c.course_title AS course_title
    FROM Courses c 

    LEFT JOIN EnrolledCourseDetails e ON
    c.course_id = e.course_id
    WHERE c.course_id IN ('515156638953610000000','586156386555710000099','676156155235810000000','740156155400610000000')  
     GROUP BY
    course_id,
    course_image_path,
    course_price,
    trainer_name,
    course_subtitle,
    course_title   
      )x inner join CourseBatch y on x.course_id=y.course_id) x group by rate ,
    enrolledcount,
    is_request,
    is_wishlist,
    is_enroll,
    review_given_by_student,
    x.course_id,
    course_image_path,
    course_price,
    trainer_name,
    course_subtitle,
    course_title

我通过了where子句括号中的课程id。我需要一个表,其中的输出必须按课程id顺序排序,就像在括号中传递的那样,而不是随机排序。
下面是我目前得到的输出,你可以参考课程id的序列,它与括号中传递的序列不匹配:output\u image
如果有人能给我提供详细的解释,我将不胜感激。
p、 s:不好意思缩进,我刚刚照样复制粘贴了这个查询,而且我是新手,所以我还在学习阶段。谢谢你的时间和帮助。

相关问题