【问题标题】:How to query for the most recent records with specific values in column如何查询列中具有特定值的最新记录
【发布时间】:2015-09-04 15:01:18
【问题描述】:

我有一个学校数据库(只有 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、学校 2 和学校 3 的最新信息。

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

另外 - 下一步是查找最近的记录,而无需命名学校 ID。随着学校数量的增长——那将是完全低效的!这可能吗?

【问题讨论】:

  • 尝试将 WHERE reports.report_id=(SELECT MAX(report_id) 替换为 WHERE reports.report_id IN (SELECT MAX(report_id)
  • 也许给这个read - 可能会给你一些新的想法。当遇到此类问题时,该页面是我最喜欢的资源之一。
  • @CG_DEV 有什么区别?
  • @ficuscr 好的 - 我会看看。谢谢。
  • "IN" ,可以搜索多个值,其中 = 只搜索一个特定的......实际上我的错,我只是看到你的子查询只返回一个值,没关系,我喜欢超级忙的工作反正哈哈

标签: php mysql


【解决方案1】:

感谢所有帮助。提供的解决方案之一建议使用 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
");?>

再次感谢任何提供帮助的人。

【讨论】:

    【解决方案2】:

    您正在寻找不在 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 位的报告。

    【讨论】:

      【解决方案3】:

      这样的查询应该可以解决问题。这是三个单独的查询,每个学校 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 个查询,因此效率不会很高。随着条目数变大,这个查询会变慢。

      【讨论】:

      • 谢谢你 - 但要避免效率低下!感谢 UNION 的提示,因为这将在短期内解决问题。
      • 再次您好...我正在尝试运行上面建议的查询...但出现错误:* 警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,给定布尔值 *。我看不到错误在哪里...
      • 好的 - 错误是因为我有一个重复的列名 'ay_id'... 该怎么办?
      猜你喜欢
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多