简体   繁体   English

如何查询列中具有特定值的最新记录

[英]How to query for the most recent records with specific values in column

I have a DB of schools (there are only 3). 我有一个学校的数据库(只有3个)。 In the DB, records are submitted regarding their performance. 在数据库中,提交有关其性能的记录。

I want an option for the user to query only the most recent entries for each school. 我希望用户可以选择仅查询每所学校的最新条目。

At the moment - I can query the most recent overall... 目前-我可以查询最近的整体...

$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

But I would like to be able to find the most recent for School 1 & School 2 & School 3 at the same time. 但我希望能够同时找到School 1&School 2&School 3的最新消息。

I can see the OR isn't giving me what I want... it just returns the latest record! 我可以看到OR并没有给我我想要的东西……它只是返回最新记录! What should the query be? 查询应该是什么?

Also - the next step is then find the most recent records without having to name the school ids at all. 另外,下一步是查找最新记录,而不必完全命名学校ID。 As the number of schools grow - that would be totally inefficient! 随着学校数量的增加-这将完全没有效率! Is that possible? 那可能吗?

A query like this should do the trick. 这样的查询应该可以解决问题。 It's three separate queries, 1 for each school, put together by the UNION command: 它是三个独立的查询,每个查询由学校的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

Technically this is running 6 queries in 1, so it will not be terribly efficient. 从技术上讲,这会在1中运行6个查询,因此效率不高。 As the number of entries gets larger, this query will get slower. 随着条目数量的增加,此查询将变慢。

Youre looking for window functions which aren't in mysql but can be emulated as follows: 您正在寻找不在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
;

This gives the top 5 reports per school. 这给出了每所学校的前5个报告。

Thanks for all help. 感谢所有帮助。 One of the solutions offered suggested a UNION query. 提供的解决方案之一建议使用UNION查询。 This resulted in a 'duplicate column' error. 这导致了“重复列”错误。 So, I renamed the columns within the database and updated the query language and it now works! 因此,我重命名了数据库中的列并更新了查询语言,现在可以使用了!

$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
");?>

Thanks again for anyone who helped. 再次感谢提供帮助的任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM