如何在

时间:2015-06-18 19:44:49

标签: php mysql

我有一个学校的数据库(只有3个)。在数据库中,会提交有关其绩效的记录。

我希望用户可以选择仅查询每所学校的最新作品。

目前 - 我可以查询最新的整体...

$result = mysqli_query($con,"SELECT * FROM reports, academicyears,schools,terms
WHERE reports.report_id=(SELECT MAX(report_id) FROM reports WHERE school_id=1 OR school_id=2 OR school_id=3)
AND academicyears.ay_id=reports.ay_id
AND schools.school_id=reports.school_id
AND terms.term_id=reports.term_id
ORDER BY reports.datesubmitted DESC

但我希望能找到最新的1学校和1年级学生。学校2&学校3同时。

我可以看到OR没有给我我想要的东西......它只是返回最新记录!该查询应该是什么?

此外 - 下一步是找到最新的记录,而不必命名学校ID。随着学校数量的增长 - 这将是完全无效的!这可能吗?

3 个答案:

答案 0 :(得分:0)

这样的查询应该可以解决问题。它是三个单独的查询,每个学校1个,由UNION命令组合在一起:

FROM (
    (
        SELECT * 
        FROM reports, academicyears, schools, terms
        WHERE reports.report_id=(
            SELECT MAX(report_id) 
            FROM reports 
            WHERE school_id=1
        )
        AND academicyears.ay_id=reports.ay_id
        AND schools.school_id=reports.school_id
        AND terms.term_id=reports.term_id
    ) UNION (
        SELECT * 
        FROM reports, academicyears, schools, terms
        WHERE reports.report_id=(
            SELECT MAX(report_id) 
            FROM reports 
            WHERE school_id=2
        )
        AND academicyears.ay_id=reports.ay_id
        AND schools.school_id=reports.school_id
        AND terms.term_id=reports.term_id
    ) UNION (
        SELECT * 
        FROM reports, academicyears, schools, terms
        WHERE reports.report_id=(
            SELECT MAX(report_id) 
            FROM reports 
            WHERE school_id=3
        )
        AND academicyears.ay_id=reports.ay_id
        AND schools.school_id=reports.school_id
        AND terms.term_id=reports.term_id
    )
) AS Latest
ORDER BY datesubmitted DESC

从技术上讲,这是在1中运行6个查询,因此它的效率不会非常高。随着条目数量的增加,此查询将变慢。

答案 1 :(得分:0)

您正在寻找不在mysql中但可以按如下方式模拟的窗口函数:

SELECT
  *
FROM
  reports, academicyears,schools,terms   ,
  (
    SELECT 
      GROUP_CONCAT(top_codes_per_group) AS top_codes
    FROM
     (
       SELECT 
      SUBSTRING_INDEX(GROUP_CONCAT(report_id.   ORDER By datesubmitted DESC), ',', 5) AS top_codes_per_group
    FROM
      reports, academicyears,schools,terms       
      WHERE school_id=1 OR school_id=2 OR.   school_id=3)
AND academicyears.ay_id=reports.ay_id
AND schools.school_id=reports.school_id
AND terms.term_id=reports.term_id GROUP BY

  ) s_top_codes_per_group
  ) s_top_codes
WHERE
  FIND_IN_SET(Code, top_codes)
ORDER BY
 School,
 Datesubmitted DESC
;

这为每所学校提供了前5名报告。

答案 2 :(得分:0)

感谢您的帮助。提供的解决方案之一建议使用UNION查询。这导致了“重复列”错误。所以,我重命名了数据库中的列并更新了查询语言,现在它可以运行了!

$result = mysqli_query($con,"SELECT * FROM (
    (
        SELECT *
        FROM reports, academicyears, schools, terms 
        WHERE reports.report_id=(
            SELECT MAX(report_id) 
            FROM reports 
            WHERE rep_school_id=1
        )
        AND academicyears.ay_id=reports.rep_ay_id
        AND schools.school_id=reports.rep_school_id
        AND terms.term_id=reports.rep_term_id
    ) UNION (
        SELECT *
        FROM reports, academicyears, schools, terms 
        WHERE reports.report_id=(
            SELECT MAX(report_id) 
            FROM reports 
            WHERE rep_school_id=2
        )
        AND academicyears.ay_id=reports.rep_ay_id
        AND schools.school_id=reports.rep_school_id
        AND terms.term_id=reports.rep_term_id
    ) UNION (
        SELECT *
        FROM reports, academicyears, schools, terms 
        WHERE reports.report_id=(
            SELECT MAX(report_id) 
            FROM reports 
            WHERE rep_school_id=3
        )
        AND academicyears.ay_id=reports.rep_ay_id
        AND schools.school_id=reports.rep_school_id
        AND terms.term_id=reports.rep_term_id
    )
) AS Latest
ORDER BY datesubmitted DESC
");?>

再次感谢任何帮助过的人。