选择 *, max(date) 在 phpMyAdmin 中有效,但在我的代码中无效

发布于 2024-08-31 18:28:36 字数 556 浏览 1 评论 0原文

好的,我的语句在 phpMyAdmin 中执行得很好,但在我的 php 页面中却不是我所期望的那样。

这是我的声明:

SELECT `egid`, `group_name` , `limit`, MAX( `date` )
  FROM employee_groups
 GROUP BY `egid`
 ORDER BY `egid` DESC ;

这是可能的表:

CREATE TABLE `employee_groups` (
  `egid` int(10) unsigned NOT NULL,
  `date` date NOT NULL,
  `group_name` varchar(50) NOT NULL,
  `limit` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`egid`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

我想提取最新的组列表,例如,如果组已更改,我只想拥有最后的更改。我需要它作为一个列表(所有组)。

OK, my statement executes well in phpMyAdmin, but not how I expect it in my php page.

This is my statement:

SELECT `egid`, `group_name` , `limit`, MAX( `date` )
  FROM employee_groups
 GROUP BY `egid`
 ORDER BY `egid` DESC ;

This is may table:

CREATE TABLE `employee_groups` (
  `egid` int(10) unsigned NOT NULL,
  `date` date NOT NULL,
  `group_name` varchar(50) NOT NULL,
  `limit` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`egid`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

I want to extract the most recent list of groups, e.g. if a group has been changed I want to have only the last change. And I need it as a list (all groups).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-09-07 18:28:37

聚合函数将在 mysql 中工作,与 sql 标准不同。要从 php 访问 max(date) 的值,您必须为其指定别名:

 SELECT `egid`, `group_name` , `limit`, MAX( `date` ) as maxdate
 FROM …

然后您可以像从 php 中选择任何其他列一样选择它,希望

 while($row = mysqli_fetch_row($result)) {
   echo htmlspecialchars($row['maxdate']);
 }

有帮助

aggregate functions will work in mysql, different to the sql standard. to access the value of max(date) from php, you have to alias it:

 SELECT `egid`, `group_name` , `limit`, MAX( `date` ) as maxdate
 FROM …

you can then select it like any other colum from php with

 while($row = mysqli_fetch_row($result)) {
   echo htmlspecialchars($row['maxdate']);
 }

hope that helps

谜兔 2024-09-07 18:28:36

您的查询可能已损坏。除非满足以下两个条件之一,否则不应选择不在分组依据中的字段:

  • 您使用聚合函数。
  • 该值在功能上取决于按列分组。

group_namelimit 这两个字段似乎违反了这些规则。这意味着您将获得这些列的不确定结果。

如果您尝试选择每组的最大值,那么您应该使用稍微不同的技术。请参阅 Quassnoi 的文章 MYSQL:为您可以使用的各种方法选择持有分组最大值的记录

这是一种方法:

SELECT  di.*
FROM    (
        SELECT   egid, MAX(date) AS date
        FROM     employee_groups d
        GROUP BY egid
        ) dd
JOIN    employee_groups di
ON      di.egid = dd.egid AND di.date = dd.date

Your query might be broken. You should not select fields that aren't in the group by unless one of the following two conditions apply:

  • You use an aggregate function.
  • The value is functionally dependant on the grouped by columns.

The two fields group_name and limit appear to break these rules. This means that you will get indeterminate results for these columns.

If you are trying to select the max per group then you should use a slightly different technique. See Quassnoi's article MYSQL: Selecting records holding a groupwise maximum for a variety of methods you could use.

Here's one way to do it:

SELECT  di.*
FROM    (
        SELECT   egid, MAX(date) AS date
        FROM     employee_groups d
        GROUP BY egid
        ) dd
JOIN    employee_groups di
ON      di.egid = dd.egid AND di.date = dd.date
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文